如何在C中使用自引用结构?

-2 c struct pointers

我正在学习C中的自引用结构.以下代码中的错误是什么?我如何使其工作?

#include<stdio.h>

struct student
{
  char grade;
  struct student *ptr_dat;  //we have a pointer of datatype struct student.
};

int main()
{
  struct student data;
  struct student data1;
  data.grade = 'C';
  data1.grade = 'B';

  data.ptr_dat = &data1;  //the address of another structure is assigned. 

  /*
   print the element grade in both structures directly.
  */
  printf("%c\n",data.grade);
  printf("%c\n",data1.grade);
  /*
   how to print the element grade in data1 using pointer
  */
  printf("%c\n",data.(*ptr_dat)); //This is error.

  return 0;

} 
Run Code Online (Sandbox Code Playgroud)

提前致谢.

Que*_*tin 6

简单的语法错误:您可以访问data1等级data.ptr_dat->grade.

  • 为清楚起见,这相当于`(*data.ptr_dat).grade`.实际上,每个人都会使用` - >`运算符. (2认同)