首页  >  文章  >  后端开发  >  在C语言中,指针是指向结构体的指针

在C语言中,指针是指向结构体的指针

王林
王林转载
2023-08-26 18:45:051412浏览

在C语言中,指针是指向结构体的指针

结构体指针保存了整个结构体的加法。

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

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

声明

以下是 C 编程中指向结构体的指针的声明 -

struct tagname *ptr;

例如 - struct Student *s -

访问

下面解释了如何访问结构体指针。

Ptr-> membername;

例如 - s->sno, s->sname, s->marks;

示例程序

以下程序显示了结构体指针的用法 - p>

#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</p><p>", st ->sno);
   printf ("name = %s</p><p>", st->sname);
   printf ("marks =%f</p><p>", st ->marks);
   getch ( );
}

输出

让我们运行上面的程序,将产生以下结果 -

enter sno, sname, marks:1 Lucky 98
details of the student are:
Number = 1
name = Lucky
marks =98.000000

示例 2

考虑另一个示例,它解释了结构体指针的功能。

 实时演示

#include<stdio.h>
struct person{
   int age;
   float weight;
};
int main(){
   struct person *personPtr, person1;
   personPtr = &person1;
   printf("Enter age: ");
   scanf("%d", &personPtr->age);
   printf("Enter weight: ");
   scanf("%f", &personPtr->weight);
   printf("Displaying:</p><p>");
   printf("Age: %d</p><p>", personPtr->age);
   printf("weight: %f", personPtr->weight);
   return 0;
}

输出

让我们运行上面的程序,将产生以下结果 -

Enter age: 45
Enter weight: 60
Displaying:
Age: 45
weight: 60.000000

以上是在C语言中,指针是指向结构体的指针的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文转载于:tutorialspoint.com。如有侵权,请联系admin@php.cn删除