-1

I have a char array and I want to check if it is null.

if(_char_array[0] != '\0') {...}

is the same as

if(_char_array != '\0') {...}

thanks

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
KostasA
  • 5,204
  • 6
  • 23
  • 29
  • 5
    No, that cannot possibly do the same. One checks a single element of the array, the other compares the address – UnholySheep Jun 17 '22 at 14:36
  • 2
    An array can't be null – user253751 Jun 17 '22 at 14:37
  • Using `'\0'` as a null pointer is aconventional at best and confusing and misleading at worst. Use `NULL` or `0` when you are comparing a pointer for null-ness. – Jonathan Leffler Jun 17 '22 at 14:38
  • 1
    Note that you should not, in general, create function, variable, tag or macro names that start with an underscore. Part of [C11 §7.1.3 Reserved identifiers](https://port70.net/~nsz/c/c11/n1570.html#7.1.3) says: — _All identifiers that begin with an underscore and either an uppercase letter or another underscore are always reserved for any use._ — _All identifiers that begin with an underscore are always reserved for use as identifiers with file scope in both the ordinary and tag name spaces._ See also [What does double underscore (`__const`) mean in C?](https://stackoverflow.com/q/1449181) – Jonathan Leffler Jun 17 '22 at 14:40
  • No, not the same. The first checks if the first char in the array is non-zero (i.e. the "string" is _not_ empty). The second checks if the array _address_ is non-zero (i.e. _not_ `NULL`). It is better written as `if (_char_array != NULL)`. It is more usual to do this if we have: `char *_char_array;` (vs. `char _char_array[1];`). The latter is still valid to do. And, it makes sense in _rare_ circumstances (e.g. we're in a standalone environment like a kernel and want to know if the array is loaded at address 0). – Craig Estey Jun 17 '22 at 14:49

4 Answers4

1

This if statement

if(_char_array[0] != '\0') {...}

checks whether a character array contains a non-empty string.

In fact there are compared two integers: the first character of the array _char_array promoted to the type int and the integer character constant '\0' that has the type int.

The statement can be rewritten also like

if( _char_array[0] ) {...}

or

if( *_char_array ) {...}

This if statement

if(_char_array != '\0') {...}

does not make a sense because any array occupies a memory extent. So converted to a pointer to its first element it can not be a null pointer.

That is in this statement there are compared a pointer to the first element of the array due to the implicit conversion of the array to a pointer to its first element and a null pointer constant.

If _char_array is initially declared as a pointer then the above if statement checks whether the pointer is not a null pointer.

The statement can be rewritten like

if( _char_array ) {...}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

In the first if statement you were checking whether the first element of _char_array is 0 or not, whereas in the second if statement you were checking address of _char_array to be 0 or not.


If the address is 0 then there would be 2 cases:

CASE 1

char *_char_array = NULL;

CASE 2

char *_char_array = malloc(<any_size>); // but malloc failed and returned `NULL`

NOTE: Statically allocated arrays never have an address of 0.

Darth-CodeX
  • 2,166
  • 1
  • 6
  • 23
  • 1
    No, static arrays _can_ have an address of 0. It's just not usual for most userspace programs. The zero page is mapped protected by the OS to trap null pointer dereferences as a _convenience_ to the app programmer. However, in an embedded system, on some arches, the interrupt vector table is placed at (hardwired to) location 0. So, we might have: `typedef void (*isr_entry)(void); isr_entry isr_table[256];` and the address of `isr_table` would be 0 (set by a linker script). – Craig Estey Jun 17 '22 at 15:02
0

An array name is not a pointer, but an identifier for a variable of type array, which is implicitly converted to a pointer to the element type. These two are not the same at all, the array will always have value.

Gnqz
  • 3,292
  • 3
  • 25
  • 35
0

While the syntax only differs by 3 characters, these do very different things once compiled.

if(_char_array[0] != '\0')

This checks if the value at index 0 within _char_array is not equal to 0.

  1. _char_array[0]
    This retrieves the first element in the array as a char
  2. (_char_array[0]) != '\0'
    This is resolved to a bool; it is true when the char from step 1 is not equal to 0.
  3. if (...)
    This one is obvious, we check if the bool from step 2 is true.

if(_char_array != '\0')

This checks if _char_array is not equal to 0.

  1. _char_array
    This represents the entire array.
    It is rare to see this syntax unless the array was declared with char* instead of char[], in which case it is equivalent to != 0 or != NULL since '\0' is the char literal for 0.
  2. (_char_array) != '\0'
    This resolves to a bool that is true when the entire char array from step 1 is not equal to 0.
  3. Same as above, we evaluate the bool from step 2.

Additionally

To echo Jonathan Leffler's comment, don't get in the habit of starting names in C or C++ with a leading underscore _.
They're reserved by the standard so doing this is usually considered undefined behaviour, as it can cause namespace collisions if you try to use a different compiler, or even a different version of the same compiler.

radj307
  • 439
  • 4
  • 11