70,024
社区成员




int main()
{
char a[100][100];
char s[100]="hello world1"; //直接用char *s = "hello world1"; 只有13的长度,而你复制了100,读取了不该读的内存,但是有可能不出错
for (int i = 0; i<100; i++) // char a[100]有效范围是a[0]-a[99],这个是关键,a[100]越界
{
strncpy(a[i],s,100); //复制到第i行, 或者strncpy(&a[i][0],s,100);
}
}
void main()
{
char a[100][100];
char *s="hello world!";
strncpy(a[0],s,strlen(s)+1);//复制到第一行
puts(a[0]);
}
#include <stdio.h>
#include <string.h>
int main()
{
char matrix[10][255];
char *constStr="Hello MM!";
strcpy(matrix[9],constStr);
printf("%s\n",matrix[9]);
return 0;
}
char s[100] = "Hello World!"; //这样拷贝100个也没问题了
char *s = {"hello world!"};
char *s = "hello world!"; //而且这个默认只有13的长度,copy100个肯定会越界
char *strcpy(char *dst, const char *src);
void *memcpy(void *dst, void *src, size_t count);