简体   繁体   English

嵌套指向结构体的指针,指向包含结构体的指针?

[英]Nested pointer to struct containing pointer to containing struct?

First, I have a struct: 首先,我有一个结构:

typedef struct {
    int type;
    union expr value;
}lval;

and union which is defined as below to save room: 和联合定义如下,以节省空间:

union expr{
    long num;
    char* err;
    char* sym;
    sexpr* sexpr1;
};

and in the sexpr* is sexpr*

typedef struct {
    int count;
    struct lval** cell;
}sexpr;

I try to use it as below, 我尝试如下使用

void lval_del(lval* v){
     lval_del(v->value.sexpr1->cell[i]); 
}

but my compiler gave me a warning: 但是我的编译器给了我一个警告:

Parameter type mismatch: Incompatible pointer types 'lval *' and 'struct anonymous::lval *'. 参数类型不匹配:指针类型'lval *'和'struct匿名:: lval *'不兼容。

When I try code like below 当我尝试如下代码

lval* lval_add(lval* v,lval* x) {
    v->value.sexpr1->cell = realloc(v->value.sexpr1->cell, sizeof(lval*)*v->value.sexpr1->count);
    v->value.sexpr1->cell[v->value.sexpr1->count-1]=x;
    return v;
}

I get the same warning. 我得到同样的警告。

I ignored the warning and compiled it successfully, but when I run it, it crashes. 我忽略了警告并成功编译了它,但是当我运行它时,它崩溃了。 I think it's the problem causing the warning. 我认为这是引起警告的问题。 How can I solve it? 我该如何解决?

Edit: the full code as I compiled it is below: 编辑:我编译时的完整代码如下:

typedef struct {
   int count;
   struct lval** cell;
}sexpr;

union expr{
    long num;
    char* err;
    char* sym;
    sexpr* sexpr1;
};

typedef struct {
    int type;
    union expr value;
}lval;

void lval_del(lval* v){
    switch (v->type) {
        case LVAL_NUM:
            break;
        case LVAL_ERR:
            free(v->value.err);
            break;
        case LVAL_SYM:
            free(v->value.sym);
            break;
        case LVAL_SEXPR:
            for (int i=0;i<v->value.sexpr1->count;i++){
                lval_del(v->value.sexpr1->cell[i]);
            }
            free(v->value.sexpr1->cell);
        default:break;
    }
}

lval* lval_add(lval* v,lval* x) {
    v->value.sexpr1->count ++;
    v->value.sexpr1->cell = realloc(v->value.sexpr1->cell,sizeof(lval*)*v->value.sexpr1->count);
    v->value.sexpr1->cell[v->value.sexpr1->count-1]=x;
    return v;
}

edit: 7.24 I killed the warning by defining them in this order. 编辑:7.24我通过按此顺序定义警告来终止警告。 A complex struct is a nightmare for me, I gave up trying to deal with the pointer problem. 复杂的结构对我来说是一场噩梦,我放弃了尝试解决指针问题的尝试。 Below is my new code, still with the bad pointer. 下面是我的新代码,但仍然带有错误的指针。

struct init_lval;
struct init_sexpr;
typedef struct init_sexpr sexpr;
union expr{
    long num;
    char* err;
    char* sym;
    sexpr* sexpr1;
};
typedef struct init_lval lval;

struct init_lval
{
    int type;
    union expr value;
};

struct init_sexpr{
   int count;
   lval** cell;
};
lval* lval_add(lval* v, lval* x) {
    v->value.sexpr1->count ++;
    v->value.sexpr1->cell = realloc(v->value.sexpr1->cell,sizeof(lval*)*v->value.sexpr1->count);
    v->value.sexpr1->cell[v->value.sexpr1->count-1]=x;
    return v;
}
void lval_del(lval* v){
    switch (v->type) {
        case LVAL_NUM:
            break;
        case LVAL_ERR:
            free(v->value.err);
            break;
        case LVAL_SYM:
            free(v->value.sym);
            break;
        case LVAL_SEXPR:
            for (int i=0;i<v->value.sexpr1->count;i++){
                lval_del(v->value.sexpr1->cell[i]);
            }
            free(v->value.sexpr1->cell);
            free(v->value.sexpr1);
        default:break;
    }
    free(v);
}

struct lval** cell; is invalid, you do not have a struct lval , you typedef ed an anonymous struct to lval . 是无效的,你没有一个struct lval ,你typedef编匿名结构来lval

Change it to lval **cell; 将其更改为lval **cell;

You also do not increment v->value.sexpr1->count in your add function, so it will always write to the same position. 您也不会在add函数中增加v->value.sexpr1->count ,因此它将始终写入相同的位置。

the problem here is with realloc. 这里的问题是与realloc。 You are trying to realloc the memory which is never malloced. 您正在尝试重新分配从未分配过的内存。 In addition you need to allocate memory to sexpr1 before using it in the expressions like v->value.sexpr1->cell 另外,在像v->value.sexpr1->cell类的表达式中使用它之前,您需要为sexpr1分配内存。

Try this: 尝试这个:

lval* lval_add(lval* v,lval* x) {
    v->value.sexpr1->count ++;
    if(v->value.sexpr1->cell == NULL) {
        v->value.sexpr1->cell = malloc(sizeof(lval*)*v->value.sexpr1->count);
    } else {
        v->value.sexpr1->cell = realloc(v->value.sexpr1->cell,sizeof(lval*)*v->value.sexpr1->count);
    }
    v->value.sexpr1->cell[v->value.sexpr1->count-1]= (struct lval*)x;
    return v;
}

And initialise the lval which is being passed to lval_add , like this: 和初始化lval正在被传递到lval_add ,就像这样:

lval *z = malloc(sizeof(lval));
z->value.sexpr1 = malloc(sizeof(sexpr));
z->value.sexpr1->cell= NULL;

Hope this helps! 希望这可以帮助!

Parameter type mismatch: Incompatible pointer types 'lval *' and 'struct anonymous::lval *'. 参数类型不匹配:指针类型'lval *'和'struct匿名:: lval *'不兼容。

The above warning is because you have an anonymous struct. 上面的警告是因为您具有匿名结构。 try: 尝试:

typedef struct lval {
    int type;
    union expr value;
};

This should resolve this warning at least. 这样至少可以解决此警告。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM