Store Information Using Structures with Dynamically Memory Allocation in C Program

07-11-17 Course- C

This program asks user to store the value of n and allocates the memory for the n structure variable dynamically using malloc() function.

Source Code Demonstrate the Dynamic Memory Allocation for Structure


#include <stdio.h>
#include<stdlib.h>
struct name {
   int a;
   char c[30];
};
int main(){
   struct name *ptr;
   int i,n;
   printf("Enter n: ");
   scanf("%d",&n);

/* Allocates the memory for n structures with pointer ptr pointing to the base address. */
   ptr=(struct name*)malloc(n*sizeof(struct name));
   for(i=0;i<n;++i){
       printf("Enter string and integer respectively:\n");
       scanf("%s%d",&(ptr+i)->c, &(ptr+i)->a);
   }
   printf("Displaying Infromation:\n");
   for(i=0;i<n;++i)
       printf("%s\t%d\t\n",(ptr+i)->c,(ptr+i)->a);
   return 0;
}

Output


Enter n: 2
Enter string and integer  respectively:
Programming
22
Enter string, integer and floating number  respectively:
Structure
33

Displaying Information:
Programming      22      
Structure        33