Help needed !!! How to pass array of structure to function?

Joined
Aug 2, 2012
Messages
7
Reaction score
0
Hi, I am prati. New to this forum.I have written one C programme for structure. In this programme i passing pointer of the array of structure to the display function.
It takes the input from the user but i am not getting proper output.I mean its display loop is not working.Please help to solve this problem.
#include<stdio.h>
#include<conio.h>
struct data{
int ID;
char name[4];
};
typedef struct data record;
void display(record *[]);
record emp[2];
int i;
void main()
{
record *ptr[2];
ptr=&emp[0];
clrscr();
for(i=0;i<2;i++)
{
printf("enter name");
scanf("%s",&emp.name);
printf("enter ID");
scanf("%d",&emp.ID);
}
display(ptr);
}
void display(record *ptr[])
{
for(i=0;i<2;i++)
{
printf("%s %d",ptr->name,ptr->ID);
}
getch();
}
 
Joined
Aug 3, 2012
Messages
1
Reaction score
0
Based on your data definition, this code is an array of struct data:
typedef struct data record;
record emp[2];

The first mistake is in lines 8, 13, 14,26. This code is array of pointer to struct data:
record *[]

but it should be a pointer to array of struct data
record (*)[]

The second mistake is in line 29, this code is an array of pointer to struct data:
ptr->name

it should be a pointer to array of struct data:
(*ptr).name

The third mistake is in line 29, don't miss \n in printf:
printf("%s %d\n" ...
 
Last edited:
Joined
Aug 2, 2012
Messages
7
Reaction score
0
Thank you very much for your reply.I modified the code and made it as a pointer to array of structure and now it is working fine.Thanks a lot!!!!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,709
Latest member
AustinMudi

Latest Threads

Top