用合适的 C 语言示例阐明指针结构
cserver side programmingprogramming更新于 2025/5/15 9:22:17
指向结构的指针保存整个结构的地址。
主要用来创建复杂的数据结构,如链接列表、树、图形等。
可以使用称为箭头运算符 ( -> ) 的特殊运算符访问结构的成员。
声明
以下是指向结构的指针的声明 −
struct tagname *ptr;
例如,struct student *s;
访问
您可以使用以下访问指向结构的指针 −
Ptr-> membername;
例如,s->sno、s->sname、s->marks;
示例
以下是指针结构的 C 程序 −
#include<stdio.h> struct student{ int sno; char sname[30]; float marks; }; main ( ){ struct student s; struct student *st; printf("enter sno, sname, marks:"); scanf ("%d%s%f", & s.sno, s.sname, &s. marks); st = &s; printf ("details of the student are"); printf ("Number = %d
", st ->sno); printf ("name = %s
", st->sname); printf ("marks =%f
", st ->marks); getch ( ); }
输出
当执行上述程序时,它会产生以下结果 −
enter sno, sname, marks:1 priya 34 details of the student areNumber = 1 name = priya marks =34.000000