简体   繁体   English

如何将指针分配给结构内的指针?

[英]How do you assign a pointer to a pointer which is within a struct?

How do you assign a pointer to a pointer which is within a struct?如何将指针分配给结构内的指针?

struct student{
    char name[24];
    int age;
    int height;
    int weight;
}

struct aClass{
    struct student *students;
    int rowsUsed;
}

void addTo(struct aClass *db, struct student *a){
    ...
}

how would I assign a pointer to another pointer that is within a struct?如何将指针分配给结构内的另一个指针?

I've tried我试过了

db -> students[db -> rowsUsed] = *a;
db -> rowsUsed = db -> rowsUsed + 1;

but it's not working.但它不起作用。

Check the types...检查类型...

db -> students[db -> rowsUsed] = a;
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^   ^
   Type is "struct student"      Type is "pointer to struct student"

So you are trying to do an assign between to objects with incompatible types.因此,您正在尝试对具有不兼容类型的对象进行分配。 That will fail.那将失败。 You need the same type on both sides of the operator.您需要在运算符的两侧使用相同的类型。

Maybe you want也许你想要

db -> students[db -> rowsUsed] = *a;
                                 ^^
                                 Now it's the struct student that a points to

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

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