C-Programming

How to use pointer as an array in the structure?

We can use the pointer as an array. To access the member variable of this type of the variable we can follow syntax of this example:

Program to store and display the name and age of n students using the concept of both structure and pointer.

#include<stdio.h>

#include<conio.h>

void main()

{

struct student

{

char name[20];

int age;

};

struct student *std;

int n,i;

clrscr();

printf(“\nplease entere the number of students:\t”);

scanf(“%d”,&n);

for(i=0;i<n;i++)

     {

printf(“\nplease enter the name of the %dth student:\t”,i+1);

scanf(“%s”,(std+i)->name);

printf(“\nplease enter the age:\t”);

scanf(“%d”,&(std+i)->age);

}

//to display

printf(“\n\n\n\nS. no.\tName\t\tAge”);

for(i=0;i<n;i++)

printf(“%d.\t%s\t%d”,i+1,*((std+i)->name), *((std+i)->age));

getch();

}

Structures and pointer (Prev Lesson)
(Next Lesson) Important Questions from Strings
Back to C-Programming

No Comments

Post a Reply

Teacher
Bhim Gautam
Role : Lecturer
Read More
error: Content is protected !!