【发布时间】:2020-05-18 13:52:22
【问题描述】:
我定义了这样的三维数组,但它无法读取其中的任何字符串?哪里有问题?谢谢!
int stuTotal, courseTotal,i,k;//a dynamic array
printf("Input the total number of students that you would like to import:");
scanf("%d", &stuTotal);
printf("Input the total number of courses that you would like to import:");
scanf("%d", &courseTotal);
char ***data = (char***)calloc(stuTotal,sizeof(char));//data
for(i = 0;i < stuTotal;i++){
data[i] = (char**)calloc(courseTotal,sizeof(char));
for (k = 0;k < courseTotal;k++){
data[i][k] = (char*)calloc(20,sizeof(char));
}
}
strcpy(data[0][0],"hello");
data[0][0] 显示为空。
【问题讨论】:
-
首先,您真的需要 3D 数组吗?其次,您使用
sizeof(char)进行所有分配。
标签: c arrays string pointers calloc