# Thread: Pointers - need help understanding code

1. ## 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. 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. 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. 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. Correct.

6. 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. 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. Originally Posted by pyroknife
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