3

I have a linked list of families. I delete one of the childs siblings like so.

p->myWife->myChildren=p->myWife->myChildren->mySibling; //makes the sibling the child so the list is not broken when deleting
delete p->myWife->myChildren->mySibling;

and later i print the child/siblings attributes based upon this

if(p->myWife->myChildren->mySibling!=NULL){
 print the childs attributes
}

Whenever I print though, it prints a weird number for the sibling (im assuming its a memory address) What do I need to do to make that pointer null?

Tyler Pfaff
  • 4,900
  • 9
  • 47
  • 62

3 Answers3

10

Deleting a pointer doesn't set it to zero. It just deallocates the memory being pointed to by the pointer. To set it to NULL you will have to set it to NULL yourself.

p->myWife->myChildren->mySibling = NULL /*defined to be zero */;
Aater Suleman
  • 2,286
  • 18
  • 11
3

deleting frees the memory referenced by the pointer. To make the pointer be NULL, assign it NULL!

p->myWife->myChildren->mySibling = NULL;
dlev
  • 48,024
  • 5
  • 125
  • 132
2

After deleting, set the pointer as NULL

p->myWife->myChildren->mySibling = NULL;
Prince John Wesley
  • 62,492
  • 12
  • 87
  • 94