MastRofDsastR MastRofDsastR - 1 year ago 65
C Question

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


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?

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download