Nesting an array of strings inside a array of structures

Discussion in 'C++' started by Amit Limaye, Apr 10, 2006.

  1. Amit  Limaye

    Amit Limaye Guest

    This is wht i want to do

    typedef struct _notificationdataformat_ {
    char *m_OID;
    char **m_OIDList;
    f1 funcptr;
    } NOTIFICATIONDATAFORMAT;

    NOTIFICATIONFORMAT OIDLIST[] = {
    { "temp",{"temp1","temp11"},&func1},
    { "temp2",{"temp21","temp22","temp23"},&func2}
    };

    The compiler does not compile this code and comes up with the error
    initializer for scalar variable requires one element

    Is there way around this or wht i m doing is completely wrong
     
    Amit Limaye, Apr 10, 2006
    #1
    1. Advertising

  2. Amit  Limaye

    Noah Roberts Guest

    Amit Limaye wrote:
    > This is wht i want to do
    >
    > typedef struct _notificationdataformat_ {
    > char *m_OID;
    > char **m_OIDList;
    > f1 funcptr;
    > } NOTIFICATIONDATAFORMAT;
    >
    > NOTIFICATIONFORMAT OIDLIST[] = {
    > { "temp",{"temp1","temp11"},&func1},
    > { "temp2",{"temp21","temp22","temp23"},&func2}
    > };


    You can't do this. The reason is how arrays work. {"xx", "xx", "xx"}
    is an array of char pointers. It is reall a char *[], not a char **.
    A char ** can point at a char *[] but there is an important distinction
    here.

    Arrays take space based on how many elements are in them. A pointer
    takes a certain set amount of space, always (different types can have
    different size pointers but a pointer to a type will always be the same
    size).

    To be able to initialize the above you would need the char ** to be
    turned into a char *[]. This is not valid because then your structure
    has no defined shape and size...just won't work.

    You could create an upper bound to your array but upper bounds are
    known to break. This would work because an array initialized with
    fewer elements than it has available fills with 0. Not a very good
    design though.

    So you could turn char ** into char *[100] for example (I believe that
    would compile) but again, I am not recommending this code...it just
    answers your question.
     
    Noah Roberts, Apr 10, 2006
    #2
    1. Advertising

  3. Amit  Limaye

    Amit Limaye Guest

    Thanks noah,
    I m trying to autogenerated code from a SNMP MIB
    which has this structure and each element in the array was the
    information structure of each message. I was just trying to avoid
    writing a specific handler for each message which it seems is not
    possible unless and until i know a max element number before hand.
    Think i will revert back to each message represented by a structure.
    But this approach leaves me with needing to write a function for each
    message i expect

    -SIGTERM
    amit
     
    Amit Limaye, Apr 10, 2006
    #3
  4. Amit  Limaye

    Noah Roberts Guest

    Amit Limaye wrote:
    > Thanks noah,
    > I m trying to autogenerated code from a SNMP MIB
    > which has this structure and each element in the array was the
    > information structure of each message. I was just trying to avoid
    > writing a specific handler for each message which it seems is not
    > possible unless and until i know a max element number before hand.
    > Think i will revert back to each message represented by a structure.
    > But this approach leaves me with needing to write a function for each
    > message i expect


    I expect there is a generic approach but it just isn't necissarily what
    you where trying here. Look for something else. Maybe it involves
    something you might find in the metaprogramming book....maybe not.
     
    Noah Roberts, Apr 10, 2006
    #4
  5. Amit  Limaye

    Phlip Guest

    Noah Roberts wrote:

    > necissarily


    Nothing personal, but these misspellings are getting a little recidivistic.

    --
    Phlip
    http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
     
    Phlip, Apr 10, 2006
    #5
    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. Replies:
    3
    Views:
    3,051
  2. Samuël van Laere

    Nesting inside label tags is not a good idea?

    Samuël van Laere, Nov 17, 2003, in forum: HTML
    Replies:
    4
    Views:
    4,518
    Samuël van Laere
    Nov 18, 2003
  3. Alfonso Morra
    Replies:
    11
    Views:
    754
    Emmanuel Delahaye
    Sep 24, 2005
  4. Technoid

    Nesting if statements in switch structures

    Technoid, Apr 4, 2006, in forum: C Programming
    Replies:
    22
    Views:
    646
    Technoid
    Apr 6, 2006
  5. Trans
    Replies:
    10
    Views:
    327
    Sean O'Halpin
    Sep 16, 2005
Loading...

Share This Page