简体   繁体   中英

what exactly happens when I do this assignment

Consider this snippet

char a[]="";

Will a NULL -pointer be assigned to the a character pointer *a ?

If not how do I check that the no string has been assigned to a ?

Will a NULL pointer be assigned to the a character pointer *a?

There is no character pointer here, but an array a of char .

a will be defined as an array of char and initialised to hold an empty-string, that is a c-string with just carrying the 0 -terminator, which is one char .

how do I check that the no string has been assigned to a?

So a will have exactly one element. This element compares equal to '\\0' , which in turn compares equal to 0 .

To test this do

#include <stdio.h> /* for puts() */
#include <string.h> /* for strlen() */

int main(void)
{
  char a[] = ""; /* The same as: char a[1] = ""; */

  /* Possibility 1: */
  if (0 == a[0]) /* alternatively use '\0' == a[0] */
  {
    puts("a is an empty string.");
  }

  /* Possibility 2: */
  if (0 == strlen(a))
  {
    puts("a has length zero.");
  }
}

a will contain 1 element:

a[0] == '\0'

Note: a is not a pointer.

First of all, a is not a character pointer, it is an array of char . There are some cases where the latter is converted to the former, but they are inherently not the same type.

That said, in this initialization, array a will be initialized with an empty string.

Empty string means, the first element will be the terminating null character, so the easiest way to check if an array contains an empty string is to compare the first element with null, like

 if (a[0] == '\0') {  /*do the action*/ }

==> Will a NULL-pointer be assigned to the a character pointer *a ?

Arrays are not pointers.

For better understanding, lets refer an example from C Standard#6.7.9p32 [emphasis mine]

EXAMPLE 8 The declaration

  char s[] = "abc", t[3] = "abc"; 

defines ''plain'' char array objects s and t whose elements are initialized with character string literals. This declaration is identical to

  char s[] = { 'a', 'b', 'c', '\\0' }, t[] = { 'a', 'b', 'c' }; 

The contents of the arrays are modifiable. On the other hand, the declaration

  char *p = "abc"; 

defines p with type ''pointer to char'' and initializes it to point to an object with type ''array of char'' with length 4 whose elements are initialized with a character string literal. If an attempt is made to use p to modify the contents of the array, the behavior is undefined.

So, this statement

char a[]="";

defines char array object a whose elements are initialized with character string literal "" . Note that "" is a string literal containing a single character '\\0' .
The above statement is equivalent to

char a[] = {'\0'};

If we omit the dimension, compiler computes it for us based on the size of the initializer (here it will be 1 because initializer is only having one character '\\0' ). So the statement is same as

char a[1] = {'\0'};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM