Structure and pointer in C programming

Structure and pointer are two important things in C language. We can use pointers to access the member of a structure. In this guide we will learn how to use pointers with structure. You will also learn dynamic memory allocation in this article.

Before getting started to structure and pointer, we recommend you to see our two guide about pointer in C and structure in C. Otherwise you will not able to understand what we have discussed here.

Now, lets see the basic syntax of using pointers to structure.

// syntax of pointer to structure or structure and pointer

struct student_name{
    member1;
    member2;
    ......
    ......
};

int main(){
    struct student_name *ptr, Jerry;
}

Accessing members using pointer

// structure and pointer, accessing members using pointers in C

#include <stdio.h>
struct struct_person{
  char name[30];
  int age;
  float weight;
};

int main(){
  struct struct_person *ptrPerson, person1;
  ptrPerson = &person1;

  printf("Enter name of person : ");
  scanf("%s", &ptrPerson -> name);

  printf("Enter his age : ");
  scanf("%d", &ptrPerson -> age);

  printf("Enter his weight : ");
  scanf("%f", &ptrPerson -> weight);

  printf("\nDisplaying person's information ..........\n");
  printf("Name : %s\n", ptrPerson -> name);
  printf("Age: %d\n", ptrPerson -> age);
  printf("weight: %.2f\n", ptrPerson -> weight);

  return 0;
}

Output of this pointer to structure program

Structure and pointer in c, dynamic memory allocation of structure in c

Here in the above program the address of person1 is stored in ptrPerson by ‘ptrPerson = &person1′. After this we can access the members of person1 using ptrPerson pointer.

However you can use (.) instead of (->) operator. Because,

ptrPerson -> name is equivalent to (ptrPerson).name and so on ……

Dynamic memory allocation of structure in C

If you don’t know about dynamic memory allocation in C programming, then it is highly recommended to see our dynamic memory allocation guide then go through this guide of structure and pointer again.

When we need to increase or decrease our memory size at runtime. We use dynamic memory allocation when we need to allocate memory during runtime of a program. Here we see a C program to allocate memory dynamically using structure. Lets see the program bellow.

// Dynamic memory allocation of structure in C

#include <stdio.h>
#include <stdlib.h>
struct person_info{
  char name[20];
  int age;
  float weight;
};

int main(){
  struct person_info *ptr;
  int num, c;

   printf("Enter the number of persons here : ");
   scanf("%d", &num);

   ptr = (struct person_info*) malloc(num * sizeof(struct person_info));

   for(c = 0; c < num; ++c){
       printf("Enter name, age and weight respectively : ");
       scanf("%s %d %f", (ptr+c)->name, &(ptr+c)->age, &(ptr+c)->weight);
   }

   printf("\nDisplaying Informations of all person .......... \n");
   for(c = 0; c < num; ++c){
       printf("Name: %s\tAge: %d\tand weight : %.2f\n", (ptr+c)->name, (ptr+c)->age, (ptr+c)->weight);
   }
   return 0;
}

Let's see the output of this program;

Enter the number of persons here : 5
Enter name, age and weight respectively : Alex 23 76
Enter name, age and weight respectively : John 21 56
Enter name, age and weight respectively : Rubi 24 82
Enter name, age and weight respectively : Lucy 19 59
Enter name, age and weight respectively : Jhuma 22 49

Displaying Informations of all person ..........
Name: Alex      Age: 23 and weight : 76.00
Name: John      Age: 21 and weight : 56.00
Name: Rubi      Age: 24 and weight : 82.00
Name: Lucy      Age: 19 and weight : 59.00
Name: Jhuma     Age: 22 and weight : 49.00

Previous pageArray of structure

Next pageStructure and function in C