Array of pointers (in general)
Wade Richards
t-wader at microsoft.UUCP
Sat Dec 9 16:16:35 AEST 1989
In article <10468 at attctc.Dallas.TX.US> bobc at attctc.Dallas.TX.US (Bob Calbridge) writes:
=} [...]
=}My immediate need requires that I simply establish an array of pointers and
=}also declare the space to which the pointers point. The data area does not
=}need to have an initial content but must be reserved. Is this possible or
=}is it necessary to have a previously defined data area specified to which
=}the pointers are directed? Am I even being clear?
=}
=}Example: I want 10 uninitialized structures defined but I want to reference
=}them through an array of pointers. Rather than give each structure a name
=}and declare the array like
=}
=}struct entry *list [] = {
=} &S1, &S2, &S3, &S4
=}};
=}
=}can I avoid having to declare the structures S1, S2, etc. elsewhere in the
=}program?
I'm not sure what exactly you want, but the obvious answer (at least to me),
is:
struct entry *list [10] ;
for( i=0; i<10; i++ ) list[i]=malloc(sizeof(struct entry));
If you want the space to be initilized at compile time, you might try:
struct entry array[10];
struct entry *list[10] = { &array[0], &array[1], ... };
-- or --
struct entry *list[10] = { array, array+1, ... };
Of course this isn't much more elegant than your solution of using S1,
S2, ...
I may be proving my ignorance (I'm too laxy to look it up now), but I'm
certain that there is no way to do this as simply as your array of
pointers to character example.
Hope this is some help...
--- Wade
More information about the Comp.lang.c
mailing list