manyu
manyu

Reputation: 485

What is the value of a NULL address location?

Why does this code print 4 as the output?

Please also provide some details to help me to understand this type of behaviour better.

int main(){ 
     int *p=NULL;
     printf("%d" ,p+1);
     return 0; 
}

Upvotes: 1

Views: 2701

Answers (5)

CB Bailey
CB Bailey

Reputation: 793259

You are getting undefined behavior. It is only valid to add an integer to a pointer if the pointer points to an element of an array or one beyond the end of an array and result of moving the pointer by the number of positions denoted by the integer also points to an element of the same array, or one beyond the end. (A non-array object can be treated as the only element of a one element array for these purposes.)

A null pointer doesn't point at an element of an array so you can't add one to it. (In C++ it is explicitly allowed to add 0 to a null pointer value and get a null pointer value as the result. (See this blog entry by Andrew Koenig at Dr. Dobb's.)

You get further undefined behavior by passing a pointer value to printf where the corresponding format specifier is %d which expects and int. %p is the correct format specifier for void*, you need an explicit cast if you want to print the pointer as an int with %d.

If your program has undefined behavior then there are no guarantees about anything that might happen. You certainly can't infer anything about the language from observing the results and it is arguable what the merit there is trying to infer properties of the implementation from such behaviour as, without further evidence in the form of observable results from programs which don't have undefined behavior or guarantees from the compiler vendor, you won't know what circumstances might cause the observed behavior to change.

Upvotes: 1

ouah
ouah

Reputation: 145919

It is worth noting that p + 1 is an invalid pointer (i.e., a pointer that is neither NULL nor it points to a valid object / one past the array object) and any use of it is undefined behavior.

printf("%d" ,p+1);

or (more correct):

printf("%p", (void *) (p + 1));

is undefined behavior.

Upvotes: 3

EricS
EricS

Reputation: 9768

You are printing the address of p, not the value of what is stored AT address p. p = NULL sets p to zero. Since it is a pointer to an integer, incrementing it by one actually increments the pointer by the size of an integer, typically 4 or 8 bytes.

Attempting to print what is stored at address zero (or even 4) via *p will typically terminate your program on most systems since that area of memory is protected.

Upvotes: 1

Bartek Banachewicz
Bartek Banachewicz

Reputation: 39400

Size of the int in C is (typically) 4 bytes. So incrementing a pointer by one unit means incrementing it's value by sizeof(int).

Also, you aren't printing the value of which pointer is directing (as this would certainly crash your program) but the value of the pointer itself. You should definitely take a look at any pointers tutorial in C (or in general).

Upvotes: 3

Ernest Friedman-Hill
Ernest Friedman-Hill

Reputation: 81734

You're setting a pointer to 0 (NULL) and then adding 1 to it; then you're converting the result to an int and printing the result. The key piece of knowledge you need here is that when you increment (add 1 to) a pointer, you actually add the size of the pointed-to object -- an int pointer is advanced to point to the next int. Since int is (apparently) 4 bytes on your platform, p is incremented to point to an address 4 bytes past where it starts.

Upvotes: 9

Related Questions