Search code examples
chexcharacter-arrays

assigning 0x00 to an array in C?


I was reading over an slide where they had this:

int current = 0;
buffer[current]=0x00;

where buffer is just a character array char buffer[300];

if 0x00 is a representation of null and we replace it with NULL buffer[current]=NULL gives the following error when compiling:

assignment makes integer from pointer without a cast

can someone please explain what does the buffer[current]=0x00; means? does this check for end of the array list?


Solution

  • NULL is a macro which expands to an implementation-defined null pointer constant.

    Both these two definitions are valid for NULL:

    #define NULL  0
    

    or

    #define NULL  ((void *) 0))
    

    if the latter is used in your compiler:

    char buffer[300];
    buffer[current]=NULL;
    

    You have an invalid program as you cannot assign a pointer value to integer object.

    ((void *) 0) is a value of a pointer type. Use 0 or the equivalent hexadecimal value 0x00 to assign 0 to an integer object.