Thread: Pointers - need help understanding code

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    40

    Pointers - need help understanding code

    Code:
    // Program to traverse a linked list#include <stdio.h>
    int main (void)
    {
    struct entry
    {
    int value;
    struct entry *next;
    };
    struct entry n1, n2, n3;
    struct entry *list_pointer = &n1;
    n1.value = 100;
    n1.next = &n2;
    n2.value = 200;
    n2.next = &n3;
    n3.value = 300;
    n3.next = (struct entry *) 0; // Mark list end with null pointer
    while ( list_pointer != (struct entry *) 0 ) {
    printf ("%i\n", list_pointer->value);
    list_pointer = list_pointer->next;
    }
    return 0;
    }

    I am having trouble understanding the "1st" time through the while loop. The "while ( list_pointer != (struct entry *) 0 )" is confusing me.

    It's saying while (list_pointer is not equal to the null entry which is n3.next)

    list_pointer=&n1 initially. List_pointer initially points to the n1 structure. But how can you set the n1 structure to != 0?

    Can you set a structure to not equal the integer 0?

  2. #2
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    When you compare a pointer value to 0 you are checking if it actually POINTS to something. IE if a pointer is 0 (more commonly referred to as NULL) then its 0 address ie, there is no address that its pointing to.

    If you tried to compare a struct entry scalar, declared like:
    Code:
    struct entry entry;
    You could not easily compare it to 0 to see if its "empty". You would have to do some logic involving each member of the stuct against some pre-determined "empty" value. So you see why using a pointer with a simple comparison against NULL (represented as 0 in this case) is preferable.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    You can't set a structure to an integer, but you can set a pointer to a structure to 0. Pointers are addresses of the thing-pointed-to, so you can set them to any numerical value.

    Code:
      struct entry *s = (struct entry *)1234560;
    is legal C. In some situations (e.g. programming on an embedded device) it's useful to be able to create pointers to any memory location, to access memory mapped peripherals.
    However running under an OS (which controls what memory the program can use) dereferencing the pointer is unlikely to do anything other than crash the program.

    Now, these are illegal C:
    Code:
     struct a;  
     a = 0; // illegal
     if (a == 0) // illegal
     if (a != 0) // illegal
       
     struct entry s = (struct entry)1234560; // illegal
    Note that these are structs, not pointers to structs. Similarely, we can compare addresses:
    Code:
     struct a;  
    
     if (&a == 0) // ok
     if (&a != 0) // ok
    It's quite common to write 0 in a pointer context as "NULL". NULL is exactly the same as 0, but helps to make it clear that it is in context of a pointer.

  4. #4
    Registered User
    Join Date
    Jan 2013
    Posts
    40
    Thank you guys.

    Oh and I think I get it. So basically list_pointer!=(structentry *) 0
    is checking to see if list_pointer is actually pointing to something. Initially it is pointing to n1 which is not empty.
    When it list_pointer becomes n3.next, it terminates because it points to 0.

    Correct?

  5. #5
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    Correct.

  6. #6
    Registered User
    Join Date
    Jan 2013
    Posts
    40
    On a side note, people have been telling me that pointers is the most difficult part of C to understand. Is that true for most people?

  7. #7
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    Thats the type of question that is likely to start a heated debate on this forum. However, in general, grasping the whens, hows, and whys of proper pointer use are definitely one of the "tougher" aspects of learning the C language...as compared to say a scripting language where memory management is handled for you and thus you can not directly address it. Often those languages have their own hurdles to jump in learning as well though so its quite open to debate.

  8. #8
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by pyroknife View Post
    n3.next = (struct entry *) 0; // Mark list end with null pointer
    while ( list_pointer != (struct entry *) 0 ) {
    [/code]

    ...

    Can you set a structure to not equal the integer 0?
    Normally we write values such as (struct entry *)0 like this:

    Code:
    n3.next = NULL;
    while (list_pointer != NULL) {
    It's also common to abbreviate it like this

    Code:
    while (list_pointer)
    But they all mean the same thing.

    As to whether it's "the most difficult part of C", it's probably a matter of representing it in a way that makes sense to you. It may help to draw each struct as a box on a page. A pointer is an arrow that points to a box.

    When you write y = &x, it means to draw an arrow from y to the item x on your page. If x has type (foo) then y has type (foo*).

    When you write *y, it means to refer to the item to which y is pointing. If y has an arrow going to x, then writing *y means the same as writing x. If y has type (foo*), then *y has type (foo).

    The notation someStruct->someMember is a shorthand for (*someStruct).someMember

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Understanding pointers
    By Meerul264 in forum C++ Programming
    Replies: 4
    Last Post: 12-29-2012, 10:23 AM
  2. Understanding pointers in C
    By monmon_4 in forum C Programming
    Replies: 7
    Last Post: 10-01-2011, 08:44 AM
  3. Understanding Pointers
    By Oonej in forum C Programming
    Replies: 23
    Last Post: 06-21-2011, 05:38 PM
  4. Help with understanding Pointers...
    By kiddo in forum C Programming
    Replies: 12
    Last Post: 03-07-2010, 06:43 PM
  5. understanding pointers
    By princez90 in forum C Programming
    Replies: 14
    Last Post: 04-19-2008, 09:56 AM