我明白这个程序没有分配足够的内存。
我需要帮助的是描述执行此代码时发生的情况的解释。
我输入“由于只分配了 4 个空间,因此没有提供足够的空间,因此会导致错误。” 这对我来说听起来不对。谢谢。
#include <stdio.h>
#include <string.h>
int main()
{
char word1[20];
char *word2;
word2 = (char*)malloc(sizeof(char)*20);
printf("Sizeof word 1: %d\n", sizeof (word1)); //This line outputs 20
printf("Sizeof word 2: %d\n", sizeof (word2)); //This line outputs 4
//before & after I used malloc
strcpy(word1, "string number 1");
strcpy(word2, "string number 2"); <---- What is this doing
printf("%s\n", word1);
printf("%s\n", word2);
}