用 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

更新於:09-03-2021

112 次瀏覽

開啟您的 職業

完成本課程並獲得認證

立即開始
廣告
© . All rights reserved.