简体   繁体   English

C从结构访问结构指针变量元素

[英]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: 我具有以下结构,并且希望能够访问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. 我收到错误:当我尝试上述2时,'*'标记和'('标记之前的预期标识符。当我尝试使用stuArr->elements->studentID = student ID而不使用*时,出现错误:请求不是结构或联合的成员“元素”。

How can I access *elements? 如何访问*元素?

Since stuArr is a pointer to a pointer, you need to dereference it with * to get to the pointer. 由于stuArr是指向指针的指针,因此需要使用*解引用以获取该指针。 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 . 而且由于elements是一个数组,因此您需要对其进行索引才能获得StudentType * ,然后可以使用它来获取studentId

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

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

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