简体   繁体   English

如何索引包含指向struct的指针的struct,该指针是malloc块

[英]how to index struct containing a pointer to pointer for struct which is malloc block

i am created a struct 我创建了一个结构

typedef struct t{
     int top;
     int value;
  }s;

and another structure 和另一种结构

typedef struct y{
      int top1;
      s **p
 }z;
  z *p1;

created a block by malloc 由malloc创建了一个块

p1 = (z*) malloc(sizeof(z));
p1->p = (s**) malloc(10 * sizeof(s));

I tried with indexing the s structured block by 我尝试用s索引结构化块

p1->p[4]->top;

but i got error. 但我得到了错误。 is there a way to index pointer to pointer type in C 有没有办法将指针指向C中的指针类型

Address them with single pointer. 用单指针解决它们。

typedef struct y{
    int top1;
    s *p; //Single pointer only
}z;

z *p1;
p1 = malloc(sizeof(z)); //Don't cast...
p1->p = malloc(10 * sizeof(s));

//And then:
p1->p[4].top;

If you still want double pointers, then: 如果你还想要双指针,那么:

typedef struct y{
    int top1;
    s **p;
} z;

z *p1;
size_t i;

p1 = malloc(sizeof(z)); //Create z structure object
p1->p = malloc(10 * sizeof(*p1->p)); //Create array of pointers to objects

//Now fill that array with your objects
for (i = 0; i < 10; i++) {
    p1->p[i] = malloc(sizeof(*p1->p[i]));
}

//Now access to property as:
p1->p[4]->top; //Do something.

The second option is less preferable but depends on you, because doing single malloc for 10 objects in a row is more efficient than doing 10x times single object and 11th time for initializing base array of pointers. 第二个选项不太适合,但取决于你,因为连续10个对象单个malloc比单个对象的10倍和第11次初始化指针的基数更有效。

In struct y{ ...; s **p } struct y{ ...; s **p } struct y{ ...; s **p } , member p represents a pointer to a pointer to structure s , and not a pointer to structure s . struct y{ ...; s **p } ,成员p表示指向结构s的指针,而不是指向结构s的指针。 So p1->p[4] is a pointer to an s , not an s itself, so you cannot access member top with accessor . 所以p1->p[4]是指向s的指针,而不是s本身,所以你不能用访问器访问成员top . . To get around the compiler error, you would have to use accessor -> , which dereferences the pointer value, but then you yield undefined behaviour at runtime, since the pointer value is not initialized. 要绕过编译器错误,您必须使用访问器-> ,它取消引用指针值,但随后在运行时产生未定义的行为,因为指针值未初始化。

To solve the problem, you could either change the data type of p to s *p (as suggested by @tilz0R), or you could adapt your code to actually deal with pointers to pointers: 要解决这个问题,您可以将p的数据类型更改为s *p (由@ tilz0R建议),或者您可以调整代码以实际处理指针指针:

typedef struct t{
    int top;
    int value;
}s;

typedef struct y{
    int top1;
    s **p;
}z;
z *p1;

int main() {
    p1 = malloc(sizeof(z));
    p1->p = malloc(10 * sizeof(s*));
    for (int i=0; i<10; i++) {
        p1->p[i] = malloc(sizeof(s));
    }

    p1->p[4]->top = 5;

}

Note that pointers to pointers are more complicated to handle, as you need an extra step/loop to initialize the pointers and to free them later on. 请注意,指针的指针处理起来比较复杂,因为您需要额外的步骤/循环来初始化指针并在以后释放它们。 So actually I'd prefer the s *p -way; 所以实际上我更喜欢s *p -way; But sometimes pointers to pointers are more appropriate, eg if particular slots of the array shall remain "empty"; 但有时指针指针更合适,例如,如果数组的特定插槽应保持“空”; then you could set the respective pointer to NULL , which would not be possible in an array of struct objects. 然后你可以将相应的指针设置为NULL ,这在struct对象数组中是不可能的。

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

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