LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-25-2013, 10:15 AM   #1
MounaRM
Member
 
Registered: Nov 2010
Posts: 35

Rep: Reputation: 14
do you the type of this variable?


hello everyone, I wrote this code, and I am having a big problem to manipulate it:
Code:
 
struct neighbor {
 	int id;
	float ph;
	struct neighbor * next;
};
typedef struct neighbor T_neighbor;
//========================================================
struct entree {
	int destination_id;
	T_neighbor  * neighbor;
	struct entree * next;
};
typedef struct entree T_entree;
 
 
 
//========data 
struct data {
	//
	T_entree *		table;
 
};typedef struct data T_data;
whenever I try to use data->table , I got a segmentation fault.I think the type of data->table is T_entree * . Have you any idea about the type of data->table ?
thanks in advance for the help.
 
Old 11-25-2013, 10:32 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,863
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
1. simplify it:
Code:
 
typedef struct neighbor {
        int id;
        float ph;
	struct neighbor * next;
} neighbor;

typedef struct entree {
	int destination_id;
	neighbor  * neighbor;
	struct entree * next;
} entree;

typedef struct data {
	entree *table;
} data;
2. Compile with flags -W -Wall -Wextra -Werror

3. Always initialize your pointers with NULL, structures with memset, eg:
Code:
entree e1;
memset (&e1, 0, sizeof (e1));
4. Use debugger

5. Quote the problematic code
 
Old 11-25-2013, 10:40 AM   #3
mina86
Member
 
Registered: Aug 2008
Distribution: Debian
Posts: 517

Rep: Reputation: 229Reputation: 229Reputation: 229
Problem is with the place you are using this data structure. Not here where it is defined. Provide a short example code where the undesired behaviour can be tested, and then someone might be able to help.
 
Old 11-25-2013, 03:18 PM   #4
MounaRM
Member
 
Registered: Nov 2010
Posts: 35

Original Poster
Rep: Reputation: 14
Quote:
Originally Posted by mina86 View Post
Problem is with the place you are using this data structure. Not here where it is defined. Provide a short example code where the undesired behaviour can be tested, and then someone might be able to help.
I wanted to add contents to data->table, so I defined a function :
Code:
void AddDestination(T_entree **pp_L,int id){
	T_entree *nouveau;
	nouveau=(T_entree*)malloc(sizeof(T_entree));
	nouveau->destination_id=id;
	nouveau->neighbor=NULL;
	nouveau->next=*pp_L;
	*pp_L=nouveau;
}
in order to test this function, I added this code to main()
Code:
T_entree *L;
L=NULL;
AddDestination(&L,3);
this code works correctly , but when I tried the same test with data->table in stead of L , it didn't work !
Code:
T_data * data = malloc(sizeof(T_data));
AddDestination(&data->table,3);
may be I should initialize table, but I have no idea how to do it ..
Any idea ?
thanks

Last edited by MounaRM; 11-25-2013 at 03:24 PM.
 
Old 11-25-2013, 03:44 PM   #5
mina86
Member
 
Registered: Aug 2008
Distribution: Debian
Posts: 517

Rep: Reputation: 229Reputation: 229Reputation: 229
Code:
nouveau=(T_entree*)malloc(sizeof(T_entree));
Don't do that. In C the cast is unnecessary and at times may lead to compiler silently ignoring stdlib.h not being included. Furthermore, I would recommend using of the variable rather then the type with sizeof, as in:
Code:
nouveau = malloc(sizeof *nouveau);
As to your problem, the below works just fine:
Code:
#include <stdlib.h>

typedef struct neighbor {
	int id;
	float ph;
	struct neighbor *next;
} T_neighbor;

typedef struct entree {
	int destination_id;
	T_neighbor  *neighbor;
	struct entree *next;
} T_entree;

typedef struct data {
	T_entree *table;
} T_data;

void AddDestination(T_entree **pp_L,int id){
	T_entree *nouveau;
	nouveau = malloc(sizeof *nouveau);
	nouveau->destination_id = id;
	nouveau->neighbor = NULL;
	nouveau->next = *pp_L;
	*pp_L = nouveau;
}

int main(void) {
	T_entree *L;
	T_data *data;

	AddDestination(&L, 3);

	data = malloc(sizeof *data);
	AddDestination(&data->table, 3);

	return 0;
}
So again, please provide a complete minimal code where we can observe the issue you are experiencing.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Determining type of a variable bayanaa Programming 9 05-19-2012 03:38 AM
[SOLVED] c++ variable type followed by * bluegospel Programming 3 07-01-2010 09:57 AM
Python variable type MTK358 Programming 6 12-08-2009 04:11 AM
C++ variable type trick... ArthurHuang Programming 4 02-15-2008 07:41 PM
understand variable type in shell script qrshat Programming 5 09-09-2006 02:12 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:55 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration