C Program to Store Information Using Structure for n Elements Dynamically

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

struct person
{
  char name[20];
  int age;
};

void main()
{
  struct person *p;
  int i,n;
  printf("Enter n: ");
  scanf("%d",&n);

/* Allocates the memory for n structures with pointer p pointing to the base address. */

  p=(struct person *)calloc(n,sizeof(struct person));
  for(i=0;i<n;++i)
  {
    printf("Enter name and age respectively:\n");
    scanf("%s%d",(p+i)->name,&(p+i)->age);
  }
  printf("\nDisplaying::\n");
  for(i=0;i<n;++i)
    printf("%s\t%d\n",(p+i)->name,(p+i)->age);
  getch();
}


OUTPUT:


No comments:

Post a Comment