How to initialise an element of array to NULL.For example if I have char *array[10]; I want last element to be NULL, so that I can pass this array to execv
Array elements are initialized to 0 if they are a numeric type ( int or double ), false if they are of type boolean , or null if they are an object type like String .
You can't initialise a char array with NULL , arrays can never be NULL . You seem to be mixing up pointers and arrays. A pointer could be initialised with NULL . char str[5] = {0};
The array will be initialized to 0 in case we provide empty initializer list or just specify 0 in the initializer list. Designated Initializer: This initializer is used when we want to initialize a range with the same value. This is used only with GCC compilers.
If you have a char[] , you can zero-out individual elements using this: char arr[10] = "foo"; arr[1] = '\0'; Note that this isn't the same as assigning NULL , since arr[1] is a char and not a pointer, you can't assign NULL to it.
To initialise an array of char*
to all NULL
s:
char* array[10] = { NULL }; /* The remaining elements are implicitly NULL. */
If you want to provide initial elements for execv()
:
char* array[10] = { "/usr/bin/ls", "-l" }; /* Again, remaining elements NULL. */
or if you wish to omit the dimension from the array declaration:
char* array[] = { "/usr/bin/ls", "-l", NULL };
NULL
is nothing but : #define NULL (void*) 0 UL
The NULL
you are talking about is nul
character which is '\0'
see man execv
page or other exec processes .. it's actually take variable number of arguments
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With