Struct and Pointer Relations

In this post, I am going to tell about struct and pointer relations. The structure is a collection of dissimilar types of elements. Now, here struct how it correlated with the pointer. Let’s see with proper example.

Here’s I am declaring pointer and struct.

``````struct name {
member1;
member2;
.
.
};
int main()
{
struct name *ptr, Harry;
}``````

Here, ptr is a pointer to `struct`.

Example: Access members using Pointer

To access member of structure using pointer by the help of arrow(->) operator.

```#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:\n");
printf("Age: %d\n", personPtr->age);
printf("weight: %f", personPtr->weight);
return 0;
}```

Output:

``````Enter age: 25
Enter weight: 45
Age: 25
weight: 45 ``````

In this example, the address of person1 is stored in the personPtr pointer using `personPtr = &person1;`.

Now, you can access the members of person1 using the `personPtr` pointer.

By the way,

• `personPtr->age` is equivalent to `(*personPtr).age`
• `personPtr->weight` is equivalent to `(*personPtr).weight`

Dynamic memory allocation of structs

It’s possible that the number of struct variables you declared isn’t enough. It’s possible that you’ll need to allocate RAM during runtime. This is how you can do it in C programming.

Example: Dynamic memory allocation of structs

```#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 first 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;
}```

When you run the program, the output will be:

Output:

``````Enter the number of persons:  3
Enter first name and age respectively:  Harry 26
Enter first name and age respectively:  Gary 38
Enter first name and age respectively:  Raksha 23
Displaying Information:
Name: Harry	Age: 24
Name: Gary	Age: 32
Name: Raksha Age: 23``````

In the above example, n number of struct variables are created where n is entered by the user.

To allocate the memory for n number of struct person, we used,

``ptr = (struct person*) malloc(n * sizeof(struct person));``

Then, we used the ptr pointer to access elements of person.

Some other examples are in structure:

Example-1

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

Output:

``````enter sno, sname, marks: 1 RAM 89.000000
details of the student are:
Number = 1
name = RAM
marks = 98.000000``````

Example-2

Consider another example that explains the functioning of pointers to structures.

```#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:\n");
printf("Age: %d\n", personPtr->age);
printf("weight: %f", personPtr->weight);
return 0;
}```

Output:

``````Enter age: 23
Enter weight: 70
Displaying:
Age: 23
weight: 40.000000``````