how to initialize a structure

Discussion in 'C Programming' started by Ramprasad A Padmanabhan, Oct 29, 2003.

  1. I have a simple structure defined like this

    struct userid {
    char uid[MAXUIDLENGTH];
    int insize;
    int outsize;
    };
    typedef struct userid user;


    Now I want to define a user array and initialize it

    I tried something like this but doesnt work

    user *list[] = {
    {"user1",10,20},
    {"user2",0,20},
    {"user3",11,2}
    };


    Is there a way I can initialize this array while its declaration.

    Thanks
    Ram
    Ramprasad A Padmanabhan, Oct 29, 2003
    #1
    1. Advertising

  2. Ramprasad A Padmanabhan

    Jirka Klaue Guest

    Ramprasad A Padmanabhan wrote:

    > I have a simple structure defined like this
    >
    > struct userid {
    > char uid[MAXUIDLENGTH];
    > int insize;
    > int outsize;
    > };
    > typedef struct userid user;
    >
    > Now I want to define a user array and initialize it
    >
    > I tried something like this but doesnt work
    >
    > user *list[] = {

    ^
    Drop this.

    > {"user1",10,20},
    > {"user2",0,20},
    > {"user3",11,2}
    > };
    >
    > Is there a way I can initialize this array while its declaration.


    struct userid {
    char uid[MAXUIDLENGTH];
    int insize, outsize;
    } list[] = {
    {"user1", 10, 20},
    {"user2", 0, 20},
    {"user3", 11, 2}
    };

    Jirka
    Jirka Klaue, Oct 29, 2003
    #2
    1. Advertising

  3. Ramprasad A Padmanabhan <> wrote:

    <snip>
    >I tried something like this but doesnt work
    >
    >user *list[] = {
    > {"user1",10,20},
    > {"user2",0,20},
    > {"user3",11,2}
    > };


    Drop the spurious * and you'll be fine.

    Regards
    --
    Irrwahn
    ()
    Irrwahn Grausewitz, Oct 29, 2003
    #3
  4. Ramprasad A Padmanabhan

    -berlin.de Guest

    Ramprasad A Padmanabhan <> wrote:
    > I have a simple structure defined like this


    > struct userid {
    > char uid[MAXUIDLENGTH];
    > int insize;
    > int outsize;
    > };
    > typedef struct userid user;


    > Now I want to define a user array and initialize it


    > I tried something like this but doesnt work


    > user *list[] = {


    This declares an array of *pointers* to such structures, but not
    an array of structures. I guess you should get rid of the '*' in
    front of 'list'.

    > {"user1",10,20},
    > {"user2",0,20},
    > {"user3",11,2}
    > };


    And that's how you would initialize an array of structures. That's
    ok when you remove the '*'.
    Regards, Jens
    --
    _ _____ _____
    | ||_ _||_ _| -berlin.de
    _ | | | | | |
    | |_| | | | | | http://www.physik.fu-berlin.de/~toerring
    \___/ens|_|homs|_|oerring
    -berlin.de, Oct 29, 2003
    #4
  5. Ramprasad A Padmanabhan

    Kevin Bracey Guest

    In message <bnohft$13rf0i$-berlin.de>
    Ramprasad A Padmanabhan <> wrote:

    > I have a simple structure defined like this
    >
    > struct userid {
    > char uid[MAXUIDLENGTH];
    > int insize;
    > int outsize;
    > };
    > typedef struct userid user;
    >
    >
    > Now I want to define a user array and initialize it
    >
    > I tried something like this but doesnt work
    >
    > user *list[] = {
    > {"user1",10,20},
    > {"user2",0,20},
    > {"user3",11,2}
    > };
    >
    >
    > Is there a way I can initialize this array while its declaration.


    You can make list an array of "user"s rather than an array of "user *"s,
    as others have suggested. Alternatively, if you really do want an array of
    pointers, in C99 you can do it with compound literals:

    user *list[] = {
    &(user) {"user1",10,20},
    &(user) {"user2",0,20},
    &(user) {"user3",11,2}
    };

    --
    Kevin Bracey, Principal Software Engineer
    Tematic Ltd Tel: +44 (0) 1223 503464
    182-190 Newmarket Road Fax: +44 (0) 1223 503458
    Cambridge, CB5 8HE, United Kingdom WWW: http://www.tematic.com/
    Kevin Bracey, Oct 29, 2003
    #5
  6. Ramprasad A Padmanabhan

    Dan Pop Guest

    In <bnohft$13rf0i$-berlin.de> Ramprasad A Padmanabhan <> writes:

    >I have a simple structure defined like this
    >
    >struct userid {
    > char uid[MAXUIDLENGTH];
    > int insize;
    > int outsize;
    >};
    >typedef struct userid user;


    Don't typedef struct's, unless you have a *good* reason for doing it.
    Saving a few keystrokes in declarations doesn't count as a good reason.

    All you can achieve with gratuitous typedef's is render the code less
    readable.

    >Now I want to define a user array and initialize it
    >
    >I tried something like this but doesnt work
    >
    >user *list[] = {
    > {"user1",10,20},
    > {"user2",0,20},
    > {"user3",11,2}
    > };
    >
    >Is there a way I can initialize this array while its declaration.


    You need to learn how to declare things in C. Your initialiser is OK,
    your declaration isn't. You're declaring an array of pointers to user,
    NOT an array of user.

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
    Dan Pop, Oct 29, 2003
    #6
  7. Ramprasad A Padmanabhan

    pete Guest

    Ramprasad A Padmanabhan wrote:
    >
    > I have a simple structure defined like this
    >
    > struct userid {
    > char uid[MAXUIDLENGTH];
    > int insize;
    > int outsize;
    > };
    > typedef struct userid user;
    >
    > Now I want to define a user array and initialize it
    >
    > I tried something like this but doesnt work
    >
    > user *list[] = {
    > {"user1",10,20},
    > {"user2",0,20},
    > {"user3",11,2}
    > };
    >
    > Is there a way I can initialize this array while its declaration.


    Program output:
    C:\Program Files\DevStudio\SharedIDE\bin\Debug>new
    user1, 10, 20
    user2, 0, 20
    user3, 11, 2

    /* BEGIN new.c */

    #include <stdio.h>

    #define LIST \
    { \
    {"user1",10,20}, \
    {"user2", 0,20}, \
    /* { "EXP",99,99}, \
    */ {"user3",11, 2} \
    /* {"user4",10,20}, \
    */}
    #define MAXUIDLENGTH (sizeof "user4")
    #define STRUCTURES (sizeof array/ sizeof *array)

    int main(void)
    {
    struct userid {
    char uid[MAXUIDLENGTH];
    int insize;
    int outsize;
    } array[] = LIST;
    size_t structure;

    for (structure = 0; structure != STRUCTURES; ++structure) {
    printf("%s, %2d, %2d\n",
    array[structure].uid,
    array[structure].insize,
    array[structure].outsize);
    }
    return 0;
    }

    /* END new.c */

    --
    pete
    pete, Oct 30, 2003
    #7
    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. Joan
    Replies:
    11
    Views:
    1,283
  2. Excluded_Middle

    Pointers to structure and array of structure.

    Excluded_Middle, Oct 24, 2004, in forum: C Programming
    Replies:
    4
    Views:
    749
    Martin Ambuhl
    Oct 26, 2004
  3. Leo Nunez
    Replies:
    3
    Views:
    1,212
    Neil Kurzman
    Feb 9, 2005
  4. Chad
    Replies:
    16
    Views:
    853
    Realos Osis
    Dec 14, 2005
  5. vduber6er
    Replies:
    3
    Views:
    523
    Frederick Gotham
    Oct 24, 2006
Loading...

Share This Page