用 C 语言中的适当示例来说明指针结构
指向结构的指针保存了整个结构的地址。
这类指针主要用于创建复杂数据结构,例如链表、树、图等。
可以使用称为箭头运算符 ( -> ) 的特殊运算符访问结构的成员。
声明
指向结构的指针声明如下 −
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
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP