c - 在 C 中 malloc 一个二维数组,其中每个条目都是一个字符串

标签 c string multidimensional-array malloc

我正在尝试在 C 中 malloc 一个二维数组,其中每个条目都是一个字符串(因此,我想是一个三维数组)。我已经阅读了很多,这是我的尝试。但是,我遇到了段错误,我真的不确定哪里出了问题。我是编程新手,所以如果我的风格不好,我深表歉意!

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int main(int argc, char *argv[])
{
    double temp, int_check;
    int gen, exit_flag=0, valid_input, valid, i, j;

    char ***S;

    printf("argc %d\n", argc);
    if(argc < 2)
    {
        printf("Please enter command line arguments of the form: a R where a is the number of generators and R are relators\n");
    exit_flag = 1;
    }
    else
    {
        valid = sscanf(argv[1], "%lg", &temp);
        int_check = temp - (int)temp;
        valid_input = ((valid != 0) && (valid != EOF)) && (int_check == 0) && (temp > 0);

        if(!valid_input)
        {
            printf("Invalid input, the number of generators must be an integer > 0\n");
            exit_flag = 1;
        }
        gen = (int)temp;

        printf("Number of generators = %d\n", gen);
    }

    if(exit_flag==0)
    {

        S = (char***)malloc(2*sizeof(char**));      /*Defintes the grid to the size required*/
        if(S == NULL) 
        {
            printf("Cannot allocate memory for the S");
        }

        for(i=0; i<2; i++)
        {
            S[i] = (char**)malloc((argc-2)*sizeof(char*));
            if(S[i] == NULL)
            {
                printf("Cannot allocate memory for the S");
            }
        }                                                   /*Grid finished being given the right size*/

        for(i=2; i<argc; i++)                               /*Put the relators in the grid. Make rhs equal 1*/
        {
            strcpy(S[0][i-2], argv[i]);
            strcpy(S[1][i-2], "1");
            printf("relator %s\n", S[0][i-2]);
        }

        printf("The array S is\n");
        for(j=0; j<(argc-2); j++)
        {
            for(i=0; i<2; i++)
            {
                printf(" %s ", S[i][j]);
            }
            printf("\n");
        }
    }

    else    /*If the inputs are invalid, exit the program*/
    {
        exit(EXIT_FAILURE);
    }

    for(i=0; i<2; i++)
    {
        free(S[i]);
    }
    free(S);

    return 0;
}

最佳答案

您永远不会为实际字符分配任何内存,而只会为指向那些所需字符的各种指针分配内存。在此处添加分配:

for(i=2; i<argc; i++)        /* Put the relators in the grid. Make rhs equal 1*/
{
    S[0][i-2] = malloc(strlen(argv[i]) + 1);  // allocate memory for the data
    strcpy(S[0][i-2], argv[i]);
    strcpy(S[1][i-2], "1");
    printf("relator %s\n", S[0][i-2]);
}

不要忘记在最后释放整个困惑。

我不确定重复的 strcpy 是否符合您的要求; strcpy 添加一个空终止符,以便您的字符串在那里结束。也许 strncpy 是对您的情况更有用的函数。

关于c - 在 C 中 malloc 一个二维数组,其中每个条目都是一个字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8965095/

相关文章:

c - 使用指针遍历内存中困惑的二进制记录列表

c++ - 使用 MPFR 计算不同精度的次正规数

c - 我试图理解的字符串指针行为

c - 在调用 va_end 之前进行 longjmp 可以吗?

c - 在C中,如何将存储在变量中的多单词字符串分解为数组,每个单词保存一个数组值

php - 基于单个值从多维数组中删除数组

python - 在 2D numpy 数组中有效地找到正值的索引范围

java - 在每个运算符/括号上拆分数学表达式

python - 是否有内置的 python 函数来计算二进制字符串中的位翻转?

c - 无法访问动态分配的二维数组