gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 19:56:13 26 4
gpt4 key购买 nike

我正在学习 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;

}

提前致谢。

最佳答案

简单语法错误:您可以使用 data.ptr_dat->grade 访问 data1 的成绩。

关于c - 如何在C中使用自引用结构?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36453176/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com