Hi !
I'm having difficulty understanding this example from a C programming book.
#include <stdio.h>
typedef struct NodeTag {
int data;
struct NodeTag *link;
}Node;
typedef Node *nodePtr;
main()
{
nodePtr ptr1 , ptr2;
ptr1 = (nodePtr) malloc (sizeof (Node));
ptr2 = (nodePtr) malloc (sizeof (Node));
ptr1->data = 19; /* P1.Data = 19 */
ptr2 = ptr1; /*Copy all of P1 to P2 call-by reference*/
ptr1->link = NULL; /*why point to nothing ? */
ptr2->link = (nodePtr) malloc (sizeof (Node));/*why do this*/
ptr1 = ptr1->link; /* P1 now points to P1(link) by
ptr1->data = 77;
ptr1->link = ptr2; /*now P1(link) points at P2 but where is P2 pointing */
ptr1->data +=3;
ptr1->link->data +=9;
printf("ptr1->data is %d\n" , ptr1->data );
printf("ptr2->data is %d\n" , ptr2->data );
printf("ptr1->link->data is %d\n" , ptr1->link->data );
printf("ptr2->link->data is %d\n" , ptr2->link->data );
printf("ptr1->link->link->data is %d\n" , ptr1->link->link->data );
}
There's 3 main thing which I don't understand:
1) What is the difference if ptr1 points at something and
ptr1->link points



LinkBack URL
About LinkBacks


