liv2hak
liv2hak

Reputation: 14990

Strange behaviour when checking for NULL character in 'C'

To clarify a point I have written two small test programs given below.

#include <stdio.h>                                                                                                                                                                       

int main(int argc, char *argv[])
{
    char *p = "ab";
    p++;
    p++;

    if (*p)
        printf("p is not NULL \n");
    else
        printf("ps is NULL \n");

    return 0;

 }

The program above initalizes the char pointer p to the string literal ab. I increment the pointer twice and then the if loop checks if p is pointing to a non-NULL character.This works fine and gives the following ouptut.

ps is NULL 

#include <stdio.h>


int main(int argc, char *argv[])
{
    char *p = '\0';                                                                                                                                                                      

    if (*p)
        printf("p is not NULL \n");
    else
        printf("ps is NULL \n");

    return 0;
}

The above program initializes the char pointer p to NULL character \0.If I compile and run the program I get segmentation fault.Can someone explain this?

The only difference in the two cases is the NULL character is at position 0 and position 2.Otherwise the program looks identical to me.

Upvotes: 0

Views: 350

Answers (4)

Greg Hewgill
Greg Hewgill

Reputation: 993163

You are initializing a char* with an int value. If you turn on warnings, your compiler should warn you about this. Try:

char *p = "\0";

with double quotes.

Note that NUL (the character with code 0) and NULL (a pointer value that doesn't point to anything) are very different things.

Upvotes: 3

AnT stands with Russia
AnT stands with Russia

Reputation: 320531

The only difference in the two cases is the NULL character is at position 0 and position 2"

That is completely false. These two programs are actually very different.

The first program initializes p with an array of type char [3]. The array decays to pointer type, which makes p to point to string "ab" stored somewhere in memory. That string ends with a \0 value at position 2, which is exactly what you observed.

The second program initializes p with a character constant \0. That constant has type int and value 0. This initialization initializes p with null-pointer value. It is equivalent to just doing

char *p = NULL;

The resultant p points nowhere. Inspecting *p causes undefined behavior.

If you want your second program to be similar to the first, you have to do it as

char *p = "\0";

or just

char *p = "";

Note the double quotes. That will make p to point to \0 value in memory. But

char *p = '\0';

with single quotes is a completely different story, as explained above.

Upvotes: 3

Sinan &#220;n&#252;r
Sinan &#220;n&#252;r

Reputation: 118128

In C, '\0' is an integer expression, so you have char *p = 0;. Therefore , p is a NULL pointer.

Also, note the distinction between a NULL pointer versus the NUL character.

See How do I get a null pointer in my programs? :

According to the language definition, an ``integral constant expression with the value 0'' in a pointer context is converted into a null pointer at compile time. That is, in an initialization, assignment, or comparison when one side is a variable or expression of pointer type, the compiler can tell that a constant 0 on the other side requests a null pointer, and generate the correctly-typed null pointer value. Therefore, the following fragments are perfectly legal:

char *p = 0;
if(p != 0)

Upvotes: 1

Keith Thompson
Keith Thompson

Reputation: 263277

It's important to understand the difference between a null pointer (NULL) and a null *character ('\0').

char *p = "ab";
p++;
p++;

This correctly sets the pointer p to point to the null character at the end of the string "ab".

char *p = '\0';

This sets p to be a null pointer. The use of '\0' as a null pointer constant is poor style, but legal (any constant integer expression with the value zero is a valid null pointer constant). The above is equivalent to the clearer:

char *p = NULL;

Any attempt to dereference p has undefined behavior, and will likely crash your program.

The only difference in the two cases is the NULL character is at position 0 and position 2.

If you want a null character (not NULL character) at position 0, you can write:

char *p = "\0";

or, nearly equivalently:

char *p = "";

The empty string consists of just the terminating '\0' null character.

Upvotes: 7

Related Questions