Pointer to array of structures
C program to show the usage of pointer to array of structures.
Program
#include<stdio.h>
#include<conio.h>
struct person
{
char name[20];
int age;
float weight;
};
void main()
{
struct person p1[10];
struct person *ptr;
int i, n;
printf("Enter the number of persons\t:");
scanf("%d",&n);
ptr = &p1;
for(i=0;i<n;i++)
{
printf("\nEnter the details of person %d",i+1);
printf("\nEnter name\t:");
scanf("%s",(ptr+i)->name);
printf("Enter age\t:");
scanf("%d",&(ptr+i)->age);
printf("Enter weight\t:");
scanf("%f",&(ptr+i)->weight);
}
for(i=0;i<n;i++)
{
printf("\n\nData of the person %d is",i+1);
printf("\nName\t:%s",(ptr+i)->name);
printf("\nAge\t:%d",(ptr+i)->age);
printf("\nWeight\t:%f",(ptr+i)->weight);
}
getch();
}
Output
Enter the number of persons :2
Enter the details of person 1
Enter name :neha
Enter age :30
Enter weight :75.3
Enter the details of person 2
Enter name :nikhil
Enter age :32
Enter weight :95.4
Data of the person 1 is
Name :neha
Age :30
Weight :75.300000
Data of the person 2 is
Name :nikhil
Age :32
Weight :95.400000
Explanation
In the above program, a structure 'person' is defined and an arraty 'p1[10]' of struct person type is declared. A struct pointer 'ptr' is also declared. The starting address of 'p1[0]' is stored in the 'ptr' pointer using
Now, you can access the members of 'p1[]' using the 'ptr' pointer. To access members of structure using the structure variable, we used the dot (.) operator. But when we have a pointer of structure type, we use arrow (->) to access structure members.
To access the members of p1[1], we will use (ptr+1) i.e.
In general, to access the members of p1[i], we will use (ptr+i) i.e.
Note that,
(ptr+i)->age is equivalent to (*(ptr+i)).age
(ptr+i)->weight is equivalent to (*(ptr+i)).weight
Program for the same is shown in "Method 2" tab.
ptr = &p1;
Now, you can access the members of 'p1[]' using the 'ptr' pointer. To access members of structure using the structure variable, we used the dot (.) operator. But when we have a pointer of structure type, we use arrow (->) to access structure members.
ptr->age
//p1[0]->age
ptr->weight
//p1[0]->weight
To access the members of p1[1], we will use (ptr+1) i.e.
(ptr+1)->age
(ptr+1)->weight
In general, to access the members of p1[i], we will use (ptr+i) i.e.
(ptr+i)->age
(ptr+i)->weight
Note that,
(ptr+i)->age is equivalent to (*(ptr+i)).age
(ptr+i)->weight is equivalent to (*(ptr+i)).weight
Program for the same is shown in "Method 2" tab.
Program
#include<stdio.h>
#include<conio.h>
struct person
{
char name[20];
int age;
float weight;
};
void main()
{
struct person p1[10];
struct person *ptr;
int i, n;
printf("Enter the number of persons\t:");
scanf("%d",&n);
ptr = p1;
for(i=0;i<n;i++)
{
printf("\nEnter the details of person %d",i+1);
printf("\nEnter name\t:");
scanf("%s",(*(ptr+i)).name);
printf("Enter age\t:");
scanf("%d",&(*(ptr+i)).age);
printf("Enter weight\t:");
scanf("%f",&(*(ptr+i)).weight);
}
for(i=0;i<n;i++)
{
printf("\n\nData of the person %d is",i+1);
printf("\nName\t:%s",(*(ptr+i)).name);
printf("\nAge\t:%d",(*(ptr+i)).age);
printf("\nWeight\t:%f",(*(ptr+i)).weight);
}
getch();
}
Output
Enter the number of persons :2
Enter the details of person 1
Enter name :neha
Enter age :30
Enter weight :75.3
Enter the details of person 2
Enter name :nikhil
Enter age :32
Enter weight :95.4
Data of the person 1 is
Name :neha
Age :30
Weight :75.300003
Data of the person 2 is
Name :nikhil
Age :32
Weight :95.400002