Pointer to an array of structures

Discussion in 'C Programming' started by Frank Münnich, Jul 11, 2003.

  1. Hi there..

    My name is Frank Münnich. I've got a question about pointers that
    refer to an array of a structure.
    How do I declare that type?

    If I have declared a structure
    struct mystruc {
    int x,y,z;
    char a,b,c;
    };

    and have furthermore declared

    mystruc data[20];

    and now I would like to have a pointer that refers to the array of
    structures, how do I do this?

    mystruc *mypointer[20] declares an array of 20 pointers, not a pointer
    referring to an array of 20 structures.

    I need this in order to pass the whole structure as a parameter in a
    function, so that the function can alter the data in the field.

    If anyone could help, it would be HIGHLY appreciated.
    Sincerely yours,

    Frank Münnich / TU Dresden
    Frank Münnich, Jul 11, 2003
    #1
    1. Advertising

  2. Frank Münnich

    Ben Pfaff Guest

    (Frank Münnich) writes:

    > My name is Frank Münnich. I've got a question about pointers that
    > refer to an array of a structure.
    > How do I declare that type?


    struct x (*p)[size];
    Ben Pfaff, Jul 11, 2003
    #2
    1. Advertising

  3. Frank Münnich

    Eric Guest

    Frank Münnich <> wrote:

    > mystruc data[20];
    >
    > and now I would like to have a pointer that refers to the array of
    > structures, how do I do this?


    You can do:

    mystruc *dataArray = &data[0];
    mystruc *dataArray = data;

    Also, you may want to read:

    http://www.eskimo.com/~scs/C-faq/q6.2.html
    http://www.eskimo.com/~scs/C-faq/q6.3.html
    http://www.eskimo.com/~scs/C-faq/q6.8.html

    --
    == Eric Gorr ========= http://www.ericgorr.net ========= ICQ:9293199 ===
    "Therefore the considerations of the intelligent always include both
    benefit and harm." - Sun Tzu
    == Insults, like violence, are the last refuge of the incompetent... ===
    Eric, Jul 11, 2003
    #3
  4. Frank Münnich

    Default User Guest

    "Frank Münnich" wrote:
    >
    > Hi there..
    >
    > My name is Frank Münnich. I've got a question about pointers that
    > refer to an array of a structure.
    > How do I declare that type?
    >
    > If I have declared a structure
    > struct mystruc {
    > int x,y,z;
    > char a,b,c;
    > };
    >
    > and have furthermore declared
    >
    > mystruc data[20];


    This is an illegal definition. Perhaps you meant

    struct mystruc data[20];


    If you are compile this as C++, stop now. You will continue to have more
    problems.

    > and now I would like to have a pointer that refers to the array of
    > structures, how do I do this?


    What book are you using that doesn't explain the array to pointer
    conversion?

    > I need this in order to pass the whole structure as a parameter in a
    > function, so that the function can alter the data in the field.



    Declare your function:

    void func (struct mystruc *funcdata);


    Then call it:

    func (data);




    Brian Rodenborn
    Default User, Jul 11, 2003
    #4
  5. "Frank Münnich" <> wrote in message
    news:-online.de...
    >
    > I've got a question about pointers that refer to an array of a structure.
    > How do I declare that type?
    >
    > If I have declared a structure
    > struct mystruc {
    > int x,y,z;
    > char a,b,c;
    > };
    >
    > and have furthermore declared
    >
    > mystruc data[20];


    That would have to be

    struct mystruc data[20];

    > and now I would like to have a pointer that refers to the array of
    > structures, how do I do this?


    struct mystruc (*mypointer)[20];

    > mystruc *mypointer[20] declares an array of 20 pointers, not a pointer
    > referring to an array of 20 structures.


    Yes, though that would be

    struct mystruc *mypointer[20];

    > I need this in order to pass the whole structure as a parameter in a
    > function, so that the function can alter the data in the field.


    If you just need to modify one structure, you could have your function take
    a pointer to a structure:

    void foo (struct mystruc *mypointer) { mypointer->x = 4; }

    In fact, if you need to modify the contents of an array of structures, you
    could just pass a pointer to the first element:

    void foo (struct mystruc *mypointer, size_t array_length)
    {
    size_t i;
    for (i = 0; i < array_length; ++i)
    {
    mypointer.x = 4;
    }
    }

    Hope that helps.

    --
    Russell Hanneken
    Russell Hanneken, Jul 11, 2003
    #5
  6. On Fri, 11 Jul 2003 19:42:34 GMT, (Frank Münnich)
    wrote:

    >Hi there..
    >
    >My name is Frank Münnich. I've got a question about pointers that
    >refer to an array of a structure.
    >How do I declare that type?
    >
    >If I have declared a structure
    >struct mystruc {
    > int x,y,z;
    > char a,b,c;
    >};
    >
    >and have furthermore declared
    >
    >mystruc data[20];
    >
    >and now I would like to have a pointer that refers to the array of
    >structures, how do I do this?
    >
    >mystruc *mypointer[20] declares an array of 20 pointers, not a pointer
    >referring to an array of 20 structures.
    >
    >I need this in order to pass the whole structure as a parameter in a
    >function, so that the function can alter the data in the field.
    >
    >If anyone could help, it would be HIGHLY appreciated.
    >Sincerely yours,
    >
    >Frank Münnich / TU Dresden


    To all those helpers out there, THANK YOU.
    You made my day, I really appreciate your work! Thanks!!
    Sincerely yours,
    Frank Münnich
    Frank Münnich, Jul 11, 2003
    #6
  7. (Frank Münnich) wrote (11 Jul 2003) in
    news:-online.de / comp.lang.c:

    > Hi there..
    >
    > My name is Frank Münnich. I've got a question about pointers that
    > refer to an array of a structure.
    > How do I declare that type?
    >
    > If I have declared a structure
    > struct mystruc {
    > int x,y,z;
    > char a,b,c;
    > };
    >
    > and have furthermore declared
    >
    > mystruc data[20];


    This is either wrong and should be
    struct mystruc data[20];
    or it is C++ and you are in the wrong place (news:comp.lang.c++ will
    serve better)

    >
    > and now I would like to have a pointer that refers to the array of
    > structures, how do I do this?


    struct mystruc *datap;

    > mystruc *mypointer[20] declares an array of 20 pointers, not a
    > pointer referring to an array of 20 structures.


    If you actually want explicitly "a pointer to an array of 20
    structures," use
    struct mystruc (*datap2)[20];
    But you may find member access a tiny bit trickier.




    --
    Martin Ambuhl
    Returning soon to the
    Fourth Largest City in America
    Martin Ambuhl, Jul 12, 2003
    #7
  8. Frank Münnich

    Malcolm Guest

    "Martin Ambuhl" <> wrote in message
    >
    > If you actually want explicitly "a pointer to an array of 20
    > structures," use
    > struct mystruc (*datap2)[20];
    > But you may find member access a tiny bit trickier.
    >

    The point is, you almost certainly don't. I don't think I've ever used such
    a construct in more than ten years of C programming.

    Even if you know that your array is necessarily 20 items long, almost all C
    programmers would take the address of the first element

    struct mystruct *datap = array;
    or
    struct mystruct *datap = &array[0];

    and then use a counter to access the members of the array;

    /* set all the x members to 100 */
    for(i=0;i<20;i++)
    datap.x = 100;
    Malcolm, Jul 12, 2003
    #8
  9. Frank Münnich

    Micah Cowan Guest

    (Frank Münnich) writes:

    > Hi there..
    >
    > My name is Frank Münnich. I've got a question about pointers that
    > refer to an array of a structure.
    > How do I declare that type?
    >
    > If I have declared a structure
    > struct mystruc {
    > int x,y,z;
    > char a,b,c;
    > };
    >
    > and have furthermore declared
    >
    > mystruc data[20];
    >
    > and now I would like to have a pointer that refers to the array of
    > structures, how do I do this?


    Declaration mirrors usage. So think how you would access the root type
    of struct mystruc once you had obtained such a type.

    First you'd have to dereference the pointer:

    *foo

    Then, you could access element n of the resulting array:

    (*foo)[n]

    NOTE: Remember that postfix operators have higher precedence than any
    others; thus, without the parens, it would be assumed that foo is an
    array of pointers, not the other way around (as you have already
    discovered). Now, you have the struct you needed! So to declare foo,
    you just use:

    struct mystruc (*data)[20];

    Note that, since declaration mirrors usage, and the postfix []
    operators bind closer than the unary *, the [] declarator
    also binds closer than the * declarator.

    HTH,
    -Micah
    Micah Cowan, Jul 14, 2003
    #9
  10. Frank Münnich

    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 2, 2012
    #10
    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. Timo
    Replies:
    6
    Views:
    7,121
    CBFalconer
    Feb 18, 2004
  2. tweak
    Replies:
    14
    Views:
    2,778
    Eric Sosman
    Jun 11, 2004
  3. mrhicks
    Replies:
    1
    Views:
    705
    Eric Sosman
    Jul 16, 2004
  4. Alfonso Morra
    Replies:
    11
    Views:
    713
    Emmanuel Delahaye
    Sep 24, 2005
  5. , India

    pointer to an array vs pointer to pointer

    , India, Sep 20, 2011, in forum: C Programming
    Replies:
    5
    Views:
    450
    James Kuyper
    Sep 23, 2011
Loading...

Share This Page