Array of Pointer to Structure

March 6, 2010 No Comments » Hits : 192






Array of Pointer to Structure

Syntax :
struct stud{    int roll;    char name[10];}*ptr[20];

Explain ?

  1. Suppose we have to maintain database of 10 students
  2. Here 10 Structures are Placed in the Memory and there base addresses are stored in Pointers .
  3. E.g ptr[0],ptr[1] stores the address of First & second structure respectively.
  4. Using the Array of Pointer the time required to access structure reduces.
  5. This is used in Dynamic Memory Allocation

Diagram :


Live Example :
#include<stdio.h>#include<conio.h>

struct stud{    int roll;    char name[10];}*ptr[10];

//-------------------------------------void main(){int i;printf("Enter the Student Details : ");

 for(i=0;i<3;i++) {   ptr[i] = (struct stud *) malloc(sizeof(struct stud));    printf("nEnter the Roll Number : ");   scanf("%d",&ptr[i]->roll);   printf("nEnter the Name        : ");   scanf("%s",ptr[i]->name); }

printf("nStudent Details are : ");

 for(i=0;i<3;i++) {   printf("nRoll Number  : %d",ptr[i]->roll);   printf("nName         : %s",ptr[i]->name); }getch();}

Output :

Enter the Student Details :Enter the Roll Number : 1Enter the Name        : Pritesh

Enter the Roll Number : 2Enter the Name        : Suraj

Enter the Roll Number : 3Enter the Name        : Aruna

Student Details are :Roll Number  : 1Name         : PriteshRoll Number  : 2Name         : SurajRoll Number  : 3Name         : Aruna

Bookmark & Share

Incoming search terms: