gpt4 book ai didi

c - C 编程中的二维字符数组

转载 作者:行者123 更新时间:2023-11-30 19:42:12 25 4
gpt4 key购买 nike

我在下面发现了奇怪的事情。我是新人,不知道这是做什么的。

#include<stdio.h>
int main(){

char y[]={"NO Error"};//This supposed to be 2D array ?! No Error! Did not write y[][]!
char x[][10]={"No Error0","No Error1","No Error2"};//This also is a 2D array! and just fine.
char z[]={"Compile time Error","Compile time Error"};//Compile time Error! not like y[]!
printf("%s ",x[2]); //Output is "No Error2"! and fine.
printf("%s ",x[2][4]); //Runtime Error!I thought output will be "E".
printf("\n%s ",y); //Output is "NO Error" ! , No Problems .
printf("\n%s ",y[0]); //Runtime Error!
return 0;
}

最佳答案

字符串就像字符数组一样工作。因此 y[] 中可以容纳单个字符串。 x[][10] 中可以容纳一个字符串数组。但在 z[] 中,字符串数组不适合。

此外,在 printf 中,%s 是字符串(实际上是 char*)。在第二个 printf 中,您使用的是 char。

关于c - C 编程中的二维字符数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32674026/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com