12

I am working on a project and I keep coming across this error that will not allow me to complete the project. When I initialize one of my pointers to point to an object that will be made during the execution of the program and I initialize it to NULL. Then when I check to see what it is set to it returns a value of nil. How is such a thing possible? I didn't believe that nil pointers existed in C. Is there any way around this?

struct order_line *front = NULL;
...
printf("Head: %p\n", front);  // prints -> Head: (nil)
iammilind
  • 68,093
  • 33
  • 169
  • 336
CF711
  • 301
  • 1
  • 5
  • 14
  • 1
    How do you check your pointer value ? Debugguer ? Printf ? This may just be a problem of representing the NULL pointer by your tool. – Sylvain Defresne Jan 20 '11 at 07:56
  • 4
    What does "nil" mean here? 0? Objective-C? – Chris Lutz Jan 20 '11 at 07:56
  • I am using printf(%p) to check it and it is in regular C not objective – CF711 Jan 20 '11 at 07:59
  • can you post some sample code with which the problem is reproducible? – Naveen Jan 20 '11 at 08:00
  • 5
    @Chris NULL pointers is an integral feature of C, and some system will just print a NULL pointer as "nil" when you use the `%p` conversion specifier. Post some code that shows the error. – nos Jan 20 '11 at 08:01
  • Could you explain if nil is the output of printf, an runtime error message, a message you got at compile time ? In the first case, I think it is normal. – shodanex Jan 20 '11 at 08:07

7 Answers7

15

%p in printf formats a pointer type. This is going to distinguish a null-pointer and print (nil) because it is a special value in the context of a pointer. If you want to output 0 for a null pointer, cast the pointer to an integer and use %d instead:

printf("Head: %d\n", (int) front);

Original answer as it may still be useful:

NULL is a macro defined as 0 or ((void *) 0), so if you set a pointer to NULL it's exactly the same as setting it to 0. This works for the purposed of declaring null pointers because the memory at address 0 will never be allocated to your program.

moinudin
  • 134,091
  • 45
  • 190
  • 216
5

use

printf("Head: %s, %d, %p\n", front, front, front);

to print Head: (null), 0, (nil)

Thanks Packia

thkala
  • 84,049
  • 23
  • 157
  • 201
user582912
  • 51
  • 1
3

When you print a pointer using printf("%p", somePtr), it is printed in an implementation-defined manner, as per this quote from the POSIX printf specification (similar wording exists in the C99 specification also).

The argument must be a pointer to void. The value of the pointer is converted to a sequence of printable characters, in an implementation-dependent manner.

I guess, that this means if the pointer is NULL, it may print it however it wants, including printing it as nil or 0x00000000 or 0.

dreamlax
  • 93,976
  • 29
  • 161
  • 209
  • well I only ever end up getting (nil) but is there any way to get around this problem because my checks for NULL are not catching this and it continues to seg fault my program – CF711 Jan 20 '11 at 08:06
  • @Chris: Convert it to an integer type and print it as an integer rather than a pointer (this is also an implementation-defined behaviour but is usually well-supported). The `uintptr_t` type defined in `` should help you. – dreamlax Jan 20 '11 at 08:08
1

I'm assuming that nil is what your debugger is telling you. In most compilers null is just #define ed to 0 anyway so that name is not that important.

rerun
  • 25,014
  • 6
  • 48
  • 78
0

After casting the structure pointer with int , it provides the expected condition with if statement ..

printf("The address of the variable is---ps->p---After Deletion-- %p \n",ps->p);
printf("The address of the variable is---ps->p---After Deletion--  %d \n",(int)ps->p);
if((int)ps->p){
printf("Again in Free\n");
doFree((void **)&ps->p);
}

OUTPUT :-

The address of the variable is---ps->p---After Deletion-- nil The address of the variable is---ps->p---After Deletion-- 0

it will evaluates to false for if condition .

Rocky
  • 1
  • 3
0

As others have stated, there is no such thing as nil pointer in C.

May be your memory allocation fails, causing 0 to be assigned to the pointer.

Donotalo
  • 12,748
  • 25
  • 83
  • 121
0

If your question is about comparing the pointer value to NULL then the value printed by printf() shouldn't matter. You can still do if(ptr==NULL) for that.

    ptr = (nil) and NULL = (nil) => ptr = NULL :)

The error you are getting must because of some other reason.
toofast1227
  • 160
  • 1
  • 15