DogNamedNibbes
DogNamedNibbes

Reputation: 13

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.

Upvotes: 1

Views: 80

Answers (1)

4386427
4386427

Reputation: 44274

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

Upvotes: 2

Related Questions