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

Discussion in 'C Programming' started by prati, Aug 3, 2012.

  1. prati

    prati

    Joined:
    Aug 2, 2012
    Messages:
    7
    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();
    }
     
    prati, Aug 3, 2012
    #1
    1. Advertising

  2. prati

    Henrique Leme

    Joined:
    Aug 3, 2012
    Messages:
    1
    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: Aug 3, 2012
    Henrique Leme, Aug 3, 2012
    #2
    1. Advertising

  3. prati

    prati

    Joined:
    Aug 2, 2012
    Messages:
    7
    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!!!!
     
    prati, Aug 4, 2012
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Panda2
    Replies:
    3
    Views:
    513
    Karl Heinz Buchegger
    Feb 24, 2005
  2. Excluded_Middle

    Pointers to structure and array of structure.

    Excluded_Middle, Oct 24, 2004, in forum: C Programming
    Replies:
    4
    Views:
    765
    Martin Ambuhl
    Oct 26, 2004
  3. prakashgkhaire@gmail.com

    how to pass a nested structure pointer to function

    prakashgkhaire@gmail.com, May 2, 2006, in forum: C Programming
    Replies:
    2
    Views:
    2,130
    Sunil Varma
    May 2, 2006
  4. bhejafry.mmk@gmail.com
    Replies:
    3
    Views:
    919
    Frederick Gotham
    Nov 30, 2006
  5. Davy
    Replies:
    9
    Views:
    1,483
Loading...

Share This Page