简体   繁体   English

我完成这项作业后会发生什么

[英]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 ? 是否将NULL指针分配给字符指针*a

If not how do I check that the no string has been assigned to a ? 如果不是我该如何检查没有字符串已被分配给a

Will a NULL pointer be assigned to the a character pointer *a? 是否将NULL指针分配给字符指针* a?

There is no character pointer here, but an array a of char . 这里没有字符指针但是 char数组 a

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 . a将被定义为char数组,并初始化为包含一个空字符串,即仅携带0终止符(即一个char )的c字符串。

how do I check that the no string has been assigned to a? 如何检查是否已将no字符串分配给a?

So a will have exactly one element. 因此, a将只有一个元素。 This element compares equal to '\\0' , which in turn compares equal to 0 . 此元素比较等于'\\0' ,后者又比较等于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将包含1个元素:

a[0] == '\0'

Note: a is not a pointer. 注意: a 不是指针。

First of all, a is not a character pointer, it is an array of char . 首先, a不是字符指针,它是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. 也就是说,在此初始化中,数组a将使用空字符串初始化。

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 ? ==>是否将NULL指针分配给a字符指针*a

Arrays are not pointers. 数组不是指针。

For better understanding, lets refer an example from C Standard#6.7.9p32 [emphasis mine] 为了更好地理解,请参考C标准#6.7.9p32中的示例[强调我的]

EXAMPLE 8 The declaration 例8声明

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

defines ''plain'' char array objects s and t whose elements are initialized with character string literals. 定义“普通” 字符数组对象 st其元素用字符串文字初始化。 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. 定义p类型为“指向char的指针” ,并将其初始化为指向长度为4的“ char数组”类型的对象,该对象的元素使用字符串文字进行初始化。 If an attempt is made to use p to modify the contents of the array, the behavior is undefined. 如果试图使用p来修改数组的内容,则该行为是不确定的。

So, this statement 所以,这句话

char a[]="";

defines char array object a whose elements are initialized with character string literal "" . 定义char 数组对象a其元素用字符串文字""初始化。 Note that "" is a string literal containing a single character '\\0' . 请注意, ""是包含单个字符'\\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' ). 如果省略尺寸,编译器将根据初始值设定项的大小为我们计算尺寸(此处将为1因为初始值设定项只有一个字符'\\0' )。 So the statement is same as 因此,声明与

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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