MastRofDsastR
MastRofDsastR

Reputation: 161

C accessing struct pointer variable element from a struct

I have the following structures, and I want be able to access elements of the StudentType:

typedef struct {
  int  studentID;
  char name[MAX_STR];
  char degree[MAX_STR];
} StudentType;

typedef struct {
  StudentType *elements[MAX_ARR];
  int size;
} StudentArrType;

My function:

void initStuArr(int studentID, char *name, char *degree, StudentType **stu, StudentArrType **stuArr){

I have tried the following both of which don't allow me to access *elements:

stuArr->*elements->studentID = studentID

and

stuArr->(*elements)->studentID = studentID

I am receiving the error: expected identifier before '*' token and '(' token when I try the 2 above. when I try stuArr->elements->studentID = student ID without using the *, I get the error: request for member 'elements' in something not a structure or union.

How can I access *elements?

Upvotes: 0

Views: 1101

Answers (1)

Barmar
Barmar

Reputation: 780663

Since stuArr is a pointer to a pointer, you need to dereference it with * to get to the pointer. And since elements is an array, you need to index it to get to a StudentType *, which you can then use to get to studentId.

(*stuArr)->elements[i]->studentId.

Upvotes: 1

Related Questions