C语言中指向结构的指针的动态内存分配

指向结构的指针包含整个结构的添加。

它用于创建复杂的数据结构,如链表、树、图等。

可以使用称为箭头运算符 ( -> ) 的特殊运算符来访问结构的成员。

声明

以下是 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*) malloc(n * sizeof(struct person));
   for(i = 0; i < n; ++i){
      printf("Enter name and age respectively: ");
      // To access members of 1st struct person,
      // ptr->name and ptr->age is used
      // To access members of 2nd struct person,
      // (ptr+1)->name and (ptr+1)->age is used
      scanf("%s %d", (ptr+i)->name, &(ptr+i)->age);
   }
   printf("Displaying Information:
");
   for(i = 0; i < n; ++i)
      printf("Name: %s\tAge: %d
", (ptr+i)->name, (ptr+i)->age);
   return 0;
}

输出

当执行上述程序时,它会产生以下结果

Enter the number of persons: 1
Enter name and age respectively: bhanu 24
Displaying Information:
Name: bhanu Age: 24

示例 2

考虑另一个关于指针和结构的例子,其中给出了一个用于演示指针和结构的 C 程序。

#include<stdio.h>
//Declaring outer and inter structures//
struct Manager{
   char Name[15];
   int Age;
   char Gender;
   float Level;
   char Role[50];
   char temp;
}m[20];
void main(){
   //Declaring variable for For loop and pointer variable//
   int i;
   struct Manager *p;
   //Defining Pointer//
   p=&m;
   //Reading User I/p//
   for (i=1;i<3;i++){//Declaring function to accept 2 manager's data//
      printf("Enter the Name of manager %d : ",i);
      gets(p->Name);
      printf("Enter the Age of manager %d : ",i);
      scanf("%d",&p->Age);
      scanf("%c",&p->temp);//Clearing Buffer//
      printf("Enter the Gender of manager %d : ",i);
      scanf("%c",&p->Gender);
      //scanf("%c",&p->temp);//Clearing Buffer//
      printf("Enter the level of manager %d : ",i);
      scanf("%f",&p->Level);
      scanf("%c",&p->temp);//Clearing Buffer//
      printf("Enter the role of manager %d : ",i);
      gets(p->Role);
      p++;
   }
   //Defining Pointer one more time to print output//
   p=&m;
   //Printing O/p//
   for (i=1;i<3;i++){
      printf("The Name of Manager %d is : %s
",i,p->Name);
      printf("The Age of Manager %d is : %d
",i,p->Age);
      printf("The Gender of Manager %d is : %c
",i,p->Gender);
      printf("The Level of Manager %d is : %f
",i,p->Level);
      printf("The Role of Manager %d is : %s
",i,p->Role);
      p++;
   }
}

输出

当执行上述程序时,它会产生以下结果

Enter the Name of manager 1 : Hari
Enter the Age of manager 1 : 55
Enter the Gender of manager 1 : M
Enter the level of manager 1 : 2
Enter the role of manager 1 : Senior
Enter the Name of manager 2 : Bob
Enter the Age of manager 2 : 60
Enter the Gender of manager 2 : M
Enter the level of manager 2 : 1
Enter the role of manager 2 : CEO
The Name of Manager 1 is : Hari
The Age of Manager 1 is : 55
The Gender of Manager 1 is : M
The Level of Manager 1 is : 2.000000
The Role of Manager 1 is : Senior
The Name of Manager 2 is : Bob
The Age of Manager 2 is : 60
The Gender of Manager 2 is : M
The Level of Manager 2 is : 1.000000
The Role of Manager 2 is : CEO
  • 14
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

新华

感谢打赏,我会继续努力原创。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值