【问题标题】:C accessing struct pointer variable element from a structC从结构访问结构指针变量元素
【发布时间】:2016-11-08 02:13:50
【问题描述】:

我有以下结构,我希望能够访问 StudentType 的元素:

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

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

我的功能:

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

我尝试了以下两种方法都不允许我访问 *elements:

stuArr->*elements->studentID = studentID

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

我收到错误:当我尝试上面的 2 时,在 '*' 标记和 '(' 标记之前的预期标识符。当我尝试 stuArr->elements->studentID = student ID 如果不使用 *,我会收到错误:在非结构或联合的东西中请求成员“元素”。

如何访问 *elements?

【问题讨论】:

    标签: c pointers struct


    【解决方案1】:

    由于stuArr 是指向指针的指针,因此您需要使用* 取消对它的引用以获取指针。由于elements 是一个数组,因此您需要对其进行索引以获取StudentType *,然后您可以使用它来获取studentId

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

    【讨论】:

      猜你喜欢
      • 2017-01-04
      • 1970-01-01
      • 2011-06-04
      • 1970-01-01
      • 2018-12-05
      • 1970-01-01
      • 1970-01-01
      • 2017-01-24
      • 2020-11-05
      相关资源
      最近更新 更多