如何使用函数中的结构指针访问结构中的二维数组?

时间:2016-10-27 20:22:47

标签: c arrays pointers struct

我是新来的,请原谅我的错误。我显然不熟悉指针或结构,一般来说,我也不是最好的程序员。但无论如何,我希望有人可以帮助我。

我试图为" 2-d数组[tag]"分配空间。在我的结构中变量" func。"由于"标签的错误引用,我似乎无法使任何任务工作。"这是产生的错误:

    prog.c: In function ‘func’:
    prog.c:33: error: assignment makes integer from pointer without a cast
    prog.c:38: error: subscripted value is neither array nor pointer
    prog.c:39: error: subscripted value is neither array nor pointer

提前致谢!

    // prog.c
    struct Str
    {
        unsigned int s;
        unsigned int e;
        unsigned long int tag;
    };


    void func(struct Str *str)
    {
        str->tag = malloc(str->s * sizeof(unsigned long int *)); // 33 str->tag error

        int i;
        for (i = 0; i < str->s; i++)
        {
            str->tag[i] = malloc(str->e * sizeof(unsigned long int)); // 38 str->tag[i] error
            memset(str->tag[i], -1, str->e * sizeof(unsigned long int)); // 39 str->tag[i] error
        }
    }

1 个答案:

答案 0 :(得分:0)

只需将tag设为双指针:

struct Str
{
    unsigned int s;
    unsigned int e;
    unsigned long int** tag;
};