By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,972 Members | 883 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,972 IT Pros & Developers. It's quick & easy.

Nesting an array of strings inside a array of structures

P: n/a
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

Apr 10 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a

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.

Apr 10 '06 #2

P: n/a
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

Apr 10 '06 #3

P: n/a

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.

Apr 10 '06 #4

P: n/a
Noah Roberts wrote:
necissarily


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

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
Apr 10 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.