C语言代码错误分析:二维字符数组和strcpy函数使用
#include<stdio.h> #include<string.h> int main() { char a[2][4]; strcpy(a[0],'you'); strcpy(a[1],'me'); a[0][3]='&'; printf("%s\n",a[0]); printf("%s\n",a[1]); return 0; }
该代码有以下两个错误:
-
数组a的定义错误,应该定义为char a[2][4],表示a是一个二维字符数组,有两行四列。
-
在使用strcpy函数时,应该指明要拷贝的目标字符串的地址,而不是数组名。因此,应该改为strcpy(a[0],'you')和strcpy(a[1],'me')。
修改后的代码如下:
#include<stdio.h> #include<string.h> int main() { char a[2][4]; strcpy(a[0],'you'); strcpy(a[1],'me'); a[0][3]='&'; printf("%s\n",a[0]); printf("%s\n",a[1]); return 0; }
原文地址: https://www.cveoy.top/t/topic/nSZk 著作权归作者所有。请勿转载和采集!