Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   having problems with this typedef statement (http://www.go4expert.com/forums/typedef-statement-t25379/)

shakisparki 30Mar2011 14:15

having problems with this typedef statement
 
Hello , Please what does the second typedef statement do,

struct listnode{
char data;
struct listnode *nextPtr;
};

typedef struct listnode Listnode;
typedef ListNode *ListNodePtr;

what is the difference between

ListNodePtr *sPtr;
ListNodePtr startPtr;

Please explain what the second Line of Code would do please
startPtr = NULL;
ListNodePtr *sPtr = &startPtr

shabbir 30Mar2011 23:19

Re: having problems with this typedef statement
 
You posted the same question as an articles two more times and try to avoid it.

shakisparki 31Mar2011 12:56

Re: having problems with this typedef statement
 
It was a mistake? can you please answer my post.

xpi0t0s 31Mar2011 18:47

Re: having problems with this typedef statement
 
The typedef defines ListNodePtr as a new type that is equivalent to ListNode *.
So the definitions:
Code:

ListNode *p1;
ListNodePtr p2;

are equivalent.

The difference between the next two is that one is a thing and the other is a pointer to that thing. This is true for any type.
Code:

TYPE someVar;
TYPE *ptr2summat;

ptr2summat is defined here as a pointer to TYPE, and someVar is an instance of TYPE.

Remember that defining a pointer to something DOES NOT create the something. So this is wrong:
Code:

int *ptr2int;
*ptr2int=5;

because ptr2int doesn't point anywhere, but this is valid:
Code:

int someInts[10];
int *ptr2int=&someInts[3]; // ptr2int points to the 4th element
*ptr2int=5;
someInts[3]=5; // equivalent to the above line

The second line of code (ListNodePtr *sPtr = &startPtr ;) defines a pointer to ListNotePtr and assigns the address of startPtr to it. sPtr here is a pointer to a pointer to ListNode, so if you want to access the ListNode at the end, you must use **sPtr.

teacher 31Mar2011 18:59

Re: having problems with this typedef statement
 
Your first doubt
simple replace Listnode with struct listnode now you have
typedef struct listnode * ListNodePtr;
now i can write
ListNodePtr x;
and x can contain the address of object of type struct listnode
for eg
Code: c++

ListNodePtr x;
ListNode y;
x=&y;

This is the same thing as
Code: c++

struct ListNode y;
struct ListNode *x;
x=&y;
x->data='e';
x->nextPtr=NULL;


in these type of problems where typedef is used and you are in confusion always try to replace as i have done above

teacher 4Apr2011 15:51

Re: having problems with this typedef statement
 
srry for late reply your second answer same as above try to replace typedef with their actual defination
you have...
ListNodePtr *sPtr;
ListNodePtr startPtr;

after replacing you get

ListNode **sPtr
ListNode *startPtr

again replace Listnode with struct listnode
struct listnode **sPtr
struct listnode *startPtr

so sPtr is a pointer to a pointer means it will carry the address of another pointer
so sPtr=&startPtr

to refernce your data you can use either of them
startPtr->data or
sPtr->startPtr->data

teacher 4Apr2011 15:58

Re: having problems with this typedef statement
 
now if you have understand your second doubt then you probably have understood the solution of your third problem
again simply replace with their original definations
Code: cpp

ListNodePtr *sPtr = &startPtr;

first replace ListNodePtr
Code: cpp

ListNode **sPtr = &startPtr;

and then you have
Code: cpp

struct listnode **sPtr = &startPtr;

again you have something similar to your second doubt

shakisparki 5Apr2011 13:13

Re: having problems with this typedef statement
 
thank y'all, i think i understand now . very resourceful.


All times are GMT +5.5. The time now is 01:06.