
How to include char arrays of pointers in structures
Quote:
>I have defined a ... structure [including]
[struct rdata {]
> char *fields[]; /* this one is the problem */
[}]
>fields is (supposed to be) a pointer to an array of char pointers
>like: char *fields[] = { str.id, str.name, ...};
Okay (deep breath): The declaration you gave in no way matches the
English description you gave, and neither one will `do what you want'.
The declaration
char *fields[];
matches the English (cdecl)
declare fields as array [ SIZE_UNKNOWN ] of pointer to char
while the English
declare fields as pointer to array [ SIZE_UNKNOWN ] of pointer
to char
matches the (essentially useless) C declaration
char *(*fields)[];
-----
Footnote: I say `essentially useless' because the only excuse for the
type `pointer to array of unknown size of T' (for any suitable type T)
is that `extern T arr[];' declares `arr' as having unknown size, and
hence `&arr' has to have such a type. The only thing that can be done
with a pointer that points to an array of unknown size is to follow it
to the first (0'th) such array, because the location of the second, third,
fourth, etc., such array is impossible to compute. In other words, given
int (*p)[];
the only thing you can `talk about' with `p' is `p[0]', and if you are
going to do that, you might as well write
int *p0 = p[0];
and then use p0 everywhere.
(One can get into a long discussion of type abstraction here; I would
argue that, at least in C, to do abstraction of this sort, you need to
use structures, not arrays. Anything else gets too messy.)
-----
Quote:
>fields is something I set up at init record time to point to the
>actual record structure elements (str) which is set up like:
>struct record {
> char id[10];
> char name[30];
> ...
> } str;
In order to point to zero or more instances of `struct record', `fields'
must have the type `pointer to struct record', i.e.,
struct record *fields;
fields = &str;
If instead you meant `fields is supposed to point to a set of pointers,
each of which points to successive elements of a ``struct record'' ',
you would first have to decide on the representation for the set of
pointers, such as `array [ PICK_SOME_SIZE ] of pointer to char':
char *ptrs[4];
ptrs[0] = str.id; /* or ptrs[0] = &str.id[0]; */
ptrs[1] = str.name;
...
If you wish, you can get the compiler to choose the size, in certain
limited circumstances:
char *ptrs[] = { str.id, str.name };
(Note that the type of `ptrs' here will be `array 2 of pointer to char',
NOT `array SIZE_UNKNOWN of pointer to char'. I recommend always noting
the size, even if you have to note that you do not know it, or that you
do not need to know it [which is often true].)
You can then make `fields' point to the first (or rather, 0'th) element
of the array `ptrs'. Because a pointer that points to the first of a
series of contiguous objects (often called `an array') can be used to
access *any one* (at a time) of those objects, this will be useful after
all. You will have to give fields a type that points to a pointer-to-char:
char **fields;
fields = ptrs;
Quote:
>The question is; how do I pass a pointer to the field array of
>char pointers which in turn point to another structures elements?
See above.
Quote:
>When I do it like above I get a warning at compile time that
>fields has zero (0) size in struct rdata. Can I ignore the warning?
No.
--
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)