指向结构的指针包含整个结构的添加。
它用于创建复杂的数据结构,如链表、树、图等。
可以使用称为箭头运算符 ( -> ) 的特殊运算符来访问结构的成员。
声明
以下是 C 编程中指向结构的指针的声明
struct tagname *ptr;
例如:struct student *s;
访问
下面将介绍如何访问指向结构的指针。
Ptr-> membername;
例如 − s->sno、s->sname、s->marks;
例
下面是一个 C 程序,它解释了 C 编程中结构的动态内存分配
#include <stdio.h>
#include <stdlib.h>
struct person {
int age;
float weight;
char name[30];
};
int main(){
struct person *ptr;
int i, n;
printf("Enter the number of persons: ");
scanf("%d", &n);
// allocating memory for n numbers of struct person
ptr = (struct person*) m