I created a series of linked lists and then printed them. Notice that I touch new->prev one time, and I verify it has been set via printf at the end of the for loop. However, when I then print all the nodes via printnode(), suddenly the ->prev pointers have all been changed to "0x11". What does that mean? How could this happen?? Where did the pointers go?
Code:
A_previous=NULL;
for (i=0; i<n_areas; i++){
new=(Area*)malloc(sizeof(Area*));
if(i==0){head=new;}
new->name=i;
new->n_exits=0;
new->next=NULL;
new->prev=A_previous;
if(A_previous!=NULL){A_previous->next=new;}
A_previous=new;
printf("[[%p]]", new->prev);}
printnode(head);
Code:
void printnode(Area *current) {
while (current) {
printf("\n<%d||[%p]>: <%p|%p>",
current->name, current, current->prev, current->next);
current = current->next;
}
}
Output:
Code:
Reading: 10 areas...[[(nil)]][[0x804a130]][[0x806b008]][[0x806b018]][[0x806b028]][[0x806b038]][[0x806b048]][[0x806b058]][[0x806b068]][[0x806b078]]
<0||[0x804a130]>: <(nil)|0x806b008>
<1||[0x806b008]>: <0x11|0x806b018>
<2||[0x806b018]>: <0x11|0x806b028>
<3||[0x806b028]>: <0x11|0x806b038>
<4||[0x806b038]>: <0x11|0x806b048>
<5||[0x806b048]>: <0x11|0x806b058>
<6||[0x806b058]>: <0x11|0x806b068>
<7||[0x806b068]>: <0x11|0x806b078>
<8||[0x806b078]>: <0x11|0x806b088>
<9||[0x806b088]>: <0x806b078|(nil)>
-Adam Rinkleff