having problems with this typedef statement

shakisparki's Avatar, Join Date: Mar 2011
Newbie Member
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
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
You posted the same question as an articles two more times and try to avoid it.
0
shakisparki's Avatar, Join Date: Mar 2011
Newbie Member
It was a mistake? can you please answer my post.
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
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.
0
teacher's Avatar, Join Date: Mar 2011
Contributor
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

Last edited by teacher; 31Mar2011 at 19:03..
0
teacher's Avatar, Join Date: Mar 2011
Contributor
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
shabbir like this
0
teacher's Avatar, Join Date: Mar 2011
Contributor
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
0
shakisparki's Avatar, Join Date: Mar 2011
Newbie Member
thank y'all, i think i understand now . very resourceful.