Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 在运行时动态创建多字符数组_C_Arrays_Multidimensional Array - Fatal编程技术网

C 在运行时动态创建多字符数组

C 在运行时动态创建多字符数组,c,arrays,multidimensional-array,C,Arrays,Multidimensional Array,我需要在运行时创建字符数组,因为问题输入如下: #define STRINGSIZE 30; int main() { int n,count; scanf("%d",&n); char **arr; arr = malloc(sizeof(char*) * n); for(int i=0; i<n; i++){ arr[i]= malloc(sizeof(char) * STRINGSIZE); } re

我需要在运行时创建字符数组,因为问题输入如下:

#define STRINGSIZE 30;
int main() {
    int n,count;
    scanf("%d",&n);
    char **arr;
    arr = malloc(sizeof(char*) * n);

    for(int i=0; i<n; i++){
        arr[i]= malloc(sizeof(char) * STRINGSIZE);
    }

    return 0;
}
1。第一行包含N个字符串。

2。接下来的N行各包含一个字符串。

我尝试创建二维阵列,但没有成功

int main() {
    int n,count;
    scanf("%d",&n);

    for(int i=0; i<n; i++){
        char* arr[i]= char*(malloc(sizeof(char)));
    }

    return 0;
}
intmain(){
int n,计数;
scanf(“%d”和“&n”);

对于(int i=0;i执行以下操作:

#define STRINGSIZE 30;
int main() {
    int n,count;
    scanf("%d",&n);
    char **arr;
    arr = malloc(sizeof(char*) * n);

    for(int i=0; i<n; i++){
        arr[i]= malloc(sizeof(char) * STRINGSIZE);
    }

    return 0;
}
而且

正如kkk在评论中指出的,检查返回值

arr = malloc(sizeof(char*) * n);
if (NULL == arr) {
    perror("Could not allocate memory");
    exit(EXIT_FAILURE);
}

这样做:

#define STRINGSIZE 30;
int main() {
    int n,count;
    scanf("%d",&n);
    char **arr;
    arr = malloc(sizeof(char*) * n);

    for(int i=0; i<n; i++){
        arr[i]= malloc(sizeof(char) * STRINGSIZE);
    }

    return 0;
}
而且

正如kkk在评论中指出的,检查返回值

arr = malloc(sizeof(char*) * n);
if (NULL == arr) {
    perror("Could not allocate memory");
    exit(EXIT_FAILURE);
}

dit以何种方式不起作用?错误:“可变大小对象可能未初始化”,创建字符数组时;可能与int数组有关,并且@NothingNothing中没有动态分配。dit以何种方式不起作用?错误:“可变大小对象可能未初始化”,创建字符数组时;可能与int数组有关,并且@NothingNothingalways检查返回中没有动态分配values@kkk修好了。谢谢。总是检查退货values@kkk修好了,谢谢。