[comp.lang.c] Array of pointers

bobc@attctc.Dallas.TX.US (Bob Calbridge) (12/04/89)

Most of what I learn of C is picked up by reading other people's code.  I
know specifically that you can define an array of pointers to predefined
character strings by the code

char *text [] = {
	"Stuff",
	"More stuff",
	"End of text"
};

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 know it's petty..........                                     =
-                  But I have to justify my salary!                           -
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

cpcahil@virtech.uucp (Conor P. Cahill) (12/04/89)

In article <10468@attctc.Dallas.TX.US>, bobc@attctc.Dallas.TX.US (Bob Calbridge) writes:
> 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?

You could just do a

	struct entry list[10]; 

and not use pointers at all.  Or, if you really need to use pointers you
could do:

	#define CNT 10

	struct entry *list[CNT];
	struct entry  reallist[CNT];

and somewhere in the begining of your program do:

	for(i=0; i < CNT; i++) list[i] = reallist+i;


-- 
+-----------------------------------------------------------------------+
| Conor P. Cahill     uunet!virtech!cpcahil      	703-430-9247	!
| Virtual Technologies Inc.,    P. O. Box 876,   Sterling, VA 22170     |
+-----------------------------------------------------------------------+

chris@mimsy.umd.edu (Chris Torek) (12/06/89)

In article <10468@attctc.Dallas.TX.US> bobc@attctc.Dallas.TX.US
(Bob Calbridge) asks about having pointers that point to objects
that do not have names:

>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 ...

The answer is `no and yes'.

C has only two kinds of unnamed objects, and only one of them is
available at compile time.  The construct

	"abc"

produces (in all but one special case) an unnamed object of type
`array N of char' (here N = 4), and evaluates to a pointer to the
first character of that array.  The other kind of unnamed object
is that returned by malloc().  All other C objects (but not necessarily
values) have names.

Someone else has already supplied the `yes' part of the answer, in
which you declare a single named object of type array N of T, and
then set your N pointer-to-T objects to point to each of these objects
in turn.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris@cs.umd.edu	Path:	uunet!mimsy!chris

t-wader@microsoft.UUCP (Wade Richards) (12/09/89)

In article <10468@attctc.Dallas.TX.US> bobc@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