Nesting an array of strings inside a array of structures

A

Amit Limaye

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
 
N

Noah Roberts

Amit said:
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.
 
A

Amit Limaye

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
 
N

Noah Roberts

Amit said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top