C Programming
In this tutorial we will learn to use pointers with array of structure variable in C programming language.
So, in the previous tutorial we learned how to create pointers for structure variable.
Let us now go ahead and create an array of structure variable and work with it via pointer variable.
In the following example we are considering the student
structure that we created in the previous tutorial and we are creating an array of student structure variable std
of size 3 to hold details of three students.
// student structure
struct student {
char id[15];
char firstname[64];
char lastname[64];
float points;
};
// student structure variable
struct student std[3];
We can represent the std
array variable as following.
Now we will create a pointer variable that will hold the starting address of the student structure variable std.
// student structure pointer variable
struct student *ptr = NULL;
// assign std to ptr
ptr = std;
Note! std
is an array variable and the name of the array variable points at the memory location so, we are assigning it to the structure pointer variable ptr
.
For this we will first set the pointer variable ptr
to point at the starting memory location of std
variable. For this we write ptr = std;
.
Then, we can increment the pointer variable using increment operator ptr++
to make the pointer point at the next element of the structure array variable i.e., from str[0] to str[1].
We will loop three times as there are three students. So, we will increment pointer variable twice. First increment will move pointer ptr
from std[0] to std[1] and the second increment will move pointer ptr
from std[1] to std[2].
To reset the pointer variable ptr
to point at the starting memory location of structure variable std
we write ptr = std;
.
#include <stdio.h>
int main(void) {
// student structure
struct student {
char id[15];
char firstname[64];
char lastname[64];
float points;
};
// student structure variable
struct student std[3];
// student structure pointer variable
struct student *ptr = NULL;
// other variables
int i;
// assign std to ptr
ptr = std;
// get detail for user
for (i = 0; i < 3; i++) {
printf("Enter detail of student #%d\n", (i + 1));
printf("Enter ID: ");
scanf("%s", ptr->id);
printf("Enter first name: ");
scanf("%s", ptr->firstname);
printf("Enter last name: ");
scanf("%s", ptr->lastname);
printf("Enter Points: ");
scanf("%f", &ptr->points);
// update pointer to point at next element
// of the array std
ptr++;
}
// reset pointer back to the starting
// address of std array
ptr = std;
for (i = 0; i < 3; i++) {
printf("\nDetail of student #%d\n", (i + 1));
// display result via std variable
printf("\nResult via std\n");
printf("ID: %s\n", std[i].id);
printf("First Name: %s\n", std[i].firstname);
printf("Last Name: %s\n", std[i].lastname);
printf("Points: %f\n", std[i].points);
// display result via ptr variable
printf("\nResult via ptr\n");
printf("ID: %s\n", ptr->id);
printf("First Name: %s\n", ptr->firstname);
printf("Last Name: %s\n", ptr->lastname);
printf("Points: %f\n", ptr->points);
// update pointer to point at next element
// of the array std
ptr++;
}
return 0;
}
Output:
Enter detail of student #1
Enter ID: s01
Enter first name: Yusuf
Enter last name: Shakeel
Enter Points: 8
Enter detail of student #2
Enter ID: s02
Enter first name: Jane
Enter last name: Doe
Enter Points: 9
Enter detail of student #3
Enter ID: s03
Enter first name: John
Enter last name: Doe
Enter Points: 6
Detail of student #1
Result via std
ID: s01
First Name: Yusuf
Last Name: Shakeel
Points: 8.000000
Result via ptr
ID: s01
First Name: Yusuf
Last Name: Shakeel
Points: 8.000000
Detail of student #2
Result via std
ID: s02
First Name: Jane
Last Name: Doe
Points: 9.000000
Result via ptr
ID: s02
First Name: Jane
Last Name: Doe
Points: 9.000000
Detail of student #3
Result via std
ID: s03
First Name: John
Last Name: Doe
Points: 6.000000
Result via ptr
ID: s03
First Name: John
Last Name: Doe
Points: 6.000000
We can represent the std
array variable in memory as follows.
Points to note!
Each student data takes 147 bytes of memory.
Member | Data Type | Size |
---|---|---|
id | char | 15 bytes |
firstname | char | 64 bytes |
lastname | char | 64 bytes |
points | float | 4 bytes |
And the array size is 3 so, total 147x3 i.e., 441 bytes is allocated to the std
array variable.
The first element std[0]
gets the memory location from 1000 to 1146.
The second element std[1]
gets the memory location from 1147 to 1293.
And the third element std[2]
gets the memory location from 1294 to 1440.
We start by first making the ptr
pointer variable point at address 1000 which is the starting address of the first element std[0]
.
Then moving forward we increment the pointer ptr++
so, it points at the memory location 1147 i.e., the starting memory location of second element std[1]
.
Similarly, in the next run we point ptr
at memory location 1294 i.e., starting location of third element std[2]
.
To access the members of the structure via pointer we use the ->
arrow operator.
ADVERTISEMENT