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
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.
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: ListNodePtr x; ListNode y; x=&y; This is the same thing as Code: 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
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
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: ListNodePtr *sPtr = &startPtr; first replace ListNodePtr Code: ListNode **sPtr = &startPtr; and then you have Code: struct listnode **sPtr = &startPtr; again you have something similar to your second doubt