C中的空字节和数组

CS *_*ent 10 c arrays

如果我声明一个像10个字符的char数组那么...

char letters[10];
Run Code Online (Sandbox Code Playgroud)

我是否创建了一组内存位置,从索引0-9表示为字符,然后第10个索引是空字节?

若是这样意味着我真的在内存中为数组创建11个位置(0到10),最后一个元素是空字节,或者我在内存中有10个位置(0到9)然后C在空格中添加空字节一个新位置(所以数组比我声明的长1个字节)?

谢谢

hac*_*cks 15

好像你和数组和字符串混淆了.
当你申报时

char letters[10] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};  
Run Code Online (Sandbox Code Playgroud)

然后它在内存位置只保留10个连续字节.

  2000  2001  2002  2003  2004  2005  2006  2007  2008  2009  //memory addresses. I assumed it is to be starting from 2000 for simplification. 
 +-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
 |     |     |     |     |     |     |     |     |     |     |
 | '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' |
 |     |     |     |     |     |     |     |     |     |     |
 +-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
Run Code Online (Sandbox Code Playgroud)

在C索引开始0.您可以访问您分配的内存位置letters[0]letters[9].访问该位置letters[10]将调用未定义的行为.但是当你宣布这样的时候

char *letters = "0123456789";  
Run Code Online (Sandbox Code Playgroud)

要么

char letters[11] = "0123456789"; 
Run Code Online (Sandbox Code Playgroud)

然后在内存中分配11个字节的空间; (NUL字符)0123456789为10 和1 \0.

 2000  2001  2002  2003  2004  2005  2006  2007  2008  2009  2010 //memory addresses. I assumed it is to be starting from 2000 for simplification. 
+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-------+
|     |     |     |     |     |     |     |     |     |     |       |
| '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' | '\0'  |
|     |     |     |     |     |     |     |     |     |     |       |
+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-------+  
                                                                ^
                                                                | NUL character   
Run Code Online (Sandbox Code Playgroud)

举一个例子

#include <stdio.h>

int main(){
   char arr[11];
   scanf("%s", arr);
   printf("%s", arr);

   return 0;
} 
Run Code Online (Sandbox Code Playgroud)

输入:

asdf  
Run Code Online (Sandbox Code Playgroud)

输出:

asdf
Run Code Online (Sandbox Code Playgroud)

现在来看看内存位置

 +-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-------+
 |     |     |     |     |     |     |     |     |     |     |       |
 | 'a' | 's' | 'd' | 'f' |'\0' |     |     |     |     |     |       |
 |     |     |     |     |     |     |     |     |     |     |       |
 +-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-------+  
Run Code Online (Sandbox Code Playgroud)

  • 现在正好开始变得更有意义了.我觉得奇怪的是C在某些情况下如何为你添加空字节而在其他情况下不会. (2认同)

Mat*_*Mat 10

我是否创建了一组内存位置,表示为索引0-9中的字符

那么第10个索引是空字节?

没有.

你预留了10 char秒的空间.没有其他的.没有什么会自动将最后一个字节设置为零,或者就像它一样.没有第11个字符可以容纳零,你只有10个.

如果您打算将其与字符串函数一起使用,那么程序员必须确保您的字符串以空值终止.(这意味着它最多可以容纳9个重要字符.)

初始化的一些常见示例:

// 10 chars exactly, not initialized - you have to take care of everything
char arr1[10];
// 10 chars exactly, all initialized - last 7 to zero - ok "C string"
char arr2[10] = { 'a', 'b', 'c' };
// three chars exactly, initialized to a, b and c - not a "C string"
char arr3[] = { 'a', 'b', 'c' };
// four chars exactly, initizalized to a, b, c and zero - ok "C string"
char arr4[] = "abc";
Run Code Online (Sandbox Code Playgroud)