Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 二维字符数组_C_Arrays_Memory_Char_Allocation - Fatal编程技术网

C 二维字符数组

C 二维字符数组,c,arrays,memory,char,allocation,C,Arrays,Memory,Char,Allocation,我是C的初学者,我正在尝试创建一个字符串数组,保存方式如下: [1] [第一串] [2] [第二串] [2] [第三轮] 我的实现如下所示: int counter = 0; char for_pole[50][50]; strcpy(for_pole[counter][50],"hello"); counter++; //then i want to print it: printf("my string: %s", for_pole[0][50]); //prints out firs

我是C的初学者,我正在尝试创建一个字符串数组,保存方式如下:

[1] [第一串]

[2] [第二串]

[2] [第三轮]

我的实现如下所示:

int counter = 0;
char for_pole[50][50];
strcpy(for_pole[counter][50],"hello");
counter++;
//then i want to print it:
printf("my string: %s", for_pole[0][50]); //prints out first string
printf("my string: %s", for_pole[1][50]); //...and second one
但返回分段错误

我应该使用一些动态内存分配吗?
正如我所说的,我是新手,很抱歉问了一个不好的问题。

我看不出在您的代码中有第二个字符串。我只看到一个字符串“你好”

您可以用以下方法编写示例

int counter = 0;
char for_pole[50][50];
strcpy(for_pole[counter],"hello");
counter++;
strcpy(for_pole[counter],"world");

//then i want to print it:
printf("my string: %s\n", for_pole[0]); //prints out first string
printf("my string: %s\n", for_pole[1]); //...and second one
至于这个表达

for_pole[counter][50]

然后1)索引50位于数组之外,因为索引的有效范围为
0-49
,2)它的类型为
char
,而不是函数
strcpy
或格式说明符
%s
所需的数组或指向char的指针。函数的printf

为零极点变量初始化

char for_pole[50][50] = {0,};
当你写作时

strcpy(for_pole[counter][50],"hello");
您正在将字符串“hello”写入字符。此外,它位于从0到49的数组的位置50上

你应该把它改成

strcpy(for_pole[counter],"hello");
因为“for_pole[i]”是第i个字符串,“for_pole[i][j]”是该字符串的第j个字符

类似地,printf应该是

printf("my string: %s", for_pole[0]);
我会添加一个“\n”以使事情更有条理

最后,代码是这样的

int counter = 0;
char for_pole[50][50];
strcpy(for_pole[counter],"hello");
counter++;

printf("my string: %s\n", for_pole[0]); //prints out first string
printf("my string: %s\n", for_pole[1]); //prints out second string

乔恩,这是有原因的。 即使根本没有bug,而是内存不足

有些人告诉你们,要注意编译器的信息和警告,是的,但这只是故事的一部分

与SEGFULT相关,我可以根据自己的经验告诉您以下几种最显著的SEGFULT:

1。您正在尝试访问不存在的索引。

数组的大小与声明数组时指定的大小完全相同。 查看此代码:

/* "Array out of bounds" error 
       valid indices for array foo
       are 0, 1, ... 999 */
  int foo[1000];
  for (int i = 0; i <= 1000 ; i++) 
    foo[i] = i; 
如果忘记初始化变量(第2种情况)或指针(第3种情况),会发生什么。答案是广泛的:从无到有到灾难

为什么会这样?因为内存是许多应用程序使用的东西,它在计算机上运行的数十万个应用程序之间共享。因此,当您创建一个变量时,它会在内存中为该变量创建一个段,但不会清除该内存(确切地说,在某些情况下,它已为空,在其他情况下-不是),只需假设该内存从未被清除。所以在这种情况下,无论是由变量还是指针(实际上也是一个变量,但这是另一回事)引起的问题,我们都说,变量存储“垃圾”

注意,要找到这些类型的SEGFAULTS并不明显,有时几乎是不可能的,所以您必须准备好应对这种情况,并且应该使用调试器(例如gdb)来消除错误

还应避免此类伪错误,例如。我的学生的实践表明,这是非常常见的错误

int n = 100;
int foo[n];
改用

int foo[100]

其他参考资料:

你误解了。。。 …字符串和数组索引如何协同工作。字符串是一个字符数组。例如,这是一个字符串:

char some_string[20] = "Hello";
这就是你打印它的方式:

printf("%s\n", some_string);
这就是如何使用strcpy():

strcpy(some_string, "Goodbye");
这就是访问字符串中第i个字符的方式:

/* Not compilable by itself; just an example. */
some_string[i]
请注意,您唯一使用
[…]
的时间是在声明它和访问字符数组中的特定字符时。但是,这是针对单个字符串的

对于字符串数组,您可以将其声明为二维字符数组,就像您使用
For_pole
所做的那样:

/* An array of 50 strings, all of which can store a maximum of 49
 * characters, plus a null termination character for each string.
 */
char for_pole[50][50];
要将
strcpy()
计数器一起使用
-th字符串:

strcpy(for_pole[counter], "hello");
printf("my string: %s", for_pole[0]);
要打印第一个(
0
-th)字符串:

要打印第一个字符串的第i个字符,请执行以下操作:

printf("string[%d]: %c", i, for_pole[0][i]);
再次,请注意我不是每次都在这里使用
[50]
。如果您有一个字符串,您可以使用其名称访问它,并使用
string[i]
访问每个字符。如果您有一个字符串数组,则可以使用其名称访问该数组,并使用
array[i]
符号访问每个字符串。要访问数组中第i个字符串的第j个字符,请执行以下操作:

/* Not compilable by itself; just an example. */
array[i][j]

为什么你的版本会出错 您不需要像在每个表达式中那样使用
[50]
。请注意访问单个字符的最后一个表达式
array[i][j]
。由于字符由整数值表示,因此编译器将在必要时将该整数转换为指针


这意味着做一些类似于strcpy的事情(对于[counter][50],“hello”)将尝试将字符串“hello”复制到编译器认为字符
代表[counter][50]
的任何内存地址。例如,如果字符是字母
A
,在我的系统上由整数65表示,那么程序将尝试将字符串“hello”的所有6个字节复制到内存地址65。我的程序没有写入该内存地址的权限,因此它会出错。这就是您遇到的问题。

编译器没有告诉您
strcpy()
的第一个参数不兼容。索引50不存在于50个元素的数组中。
for_pole[counter][50]
for_pole[counter]
的第51个字符,这是
计数器的第个字符串(从0开始)。所以它是
计数器第51个字符(从0开始)。而不是整个字符串。
strcpy(for_pole[counter], "hello");
printf("my string: %s", for_pole[0]);
printf("string[%d]: %c", i, for_pole[0][i]);
/* Not compilable by itself; just an example. */
array[i][j]