
-----------------------------------
unoho
Tue Apr 27, 2010 10:33 pm

pointer output confusion
-----------------------------------
[code]

#include 
#include 
typedef struct road_trip
{
	char place [30];
	struct road_trip* next;
}trip;

int
main (void)
{
	trip s1, s2, s3, s4, s5;
	trip *ptr;
strcpy (s1.place, "Long Point");
strcpy (s2.place, "Niagara Falls");
strcpy (s3.place, "Pelee Island");
strcpy (s4.place, "Port Burwell");
strcpy (s5.place, "Port Dover");
s5.next = &s1;
ptr = &s2;
s1.next = &s4;
s3.next = NULL;
s4.next = &s3;
s2.next = &s5;
while (ptr != NULL)
{
printf ("%s\n", ptr -> place);
ptr = ptr -> next;
}
return (0);
}

[/code]

output is:
Niagara Falls
Port Dover
Long Point
port burwell
pelee island

could anyone please explain the output to me. im kinda lost,, maybe im overthinking
thanks

-----------------------------------
TerranceN
Wed Apr 28, 2010 3:03 pm

RE:pointer output confusion
-----------------------------------
At the start of the program:

ptr goes to NULL

Long Point          goes to NULL
Niagara Falls      goes to NULL
Pelee Island       goes to NULL
Port Burwell       goes to NULL
Port Dover         goes to NULL

After you change ptr and next values:

ptr goes to Niagara Falls

Long Point          goes to Port Burwell
Niagara Falls      goes to Port Dover
Pelee Island       goes to NULL
Port Burwell       goes to Pelee Island
Port Dover         goes to Long Point

Now just follow from ptr until NULL. In case you didn't know this, what you are creating is called a linked list. Hope that helps.

-----------------------------------
unoho
Wed Apr 28, 2010 4:10 pm

RE:pointer output confusion
-----------------------------------
oh,, I already got it but thanks for your answer...
I just had to look up some notes on pointers,,

thnks again :)
