ตัวชี้ไปยังโครงสร้างถือเป็นส่วนเสริมของโครงสร้างทั้งหมด
ใช้เพื่อสร้างโครงสร้างข้อมูลที่ซับซ้อน เช่น รายการเชื่อมโยง ต้นไม้ กราฟ และอื่นๆ
สามารถเข้าถึงสมาชิกของโครงสร้างได้โดยใช้ตัวดำเนินการพิเศษที่เรียกว่าตัวดำเนินการลูกศร ( -> )
ประกาศ
ต่อไปนี้เป็นคำประกาศสำหรับตัวชี้ไปยังโครงสร้างในการเขียนโปรแกรม C -
struct tagname *ptr;
ตัวอย่างเช่น:struct นักเรียน *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:\n");
for(i = 0; i < n; ++i)
printf("Name: %s\tAge: %d\n", (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\n",i,p->Name);
printf("The Age of Manager %d is : %d\n",i,p->Age);
printf("The Gender of Manager %d is : %c\n",i,p->Gender);
printf("The Level of Manager %d is : %f\n",i,p->Level);
printf("The Role of Manager %d is : %s\n",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