Initializing a pointer inside a struct

Chris Torek torek at elf.ee.lbl.gov
Thu Apr 18 02:15:51 AEST 1991


In article <1991Apr17.105139.16331 at fy.chalmers.se> f90angu at fy.chalmers.se
(Andreas Gunnarsson) writes:
>I've tried this:
>
>struct {
>   ...
>   int *int_list;
>   ...
>} list_of_structs[] =
>{
>   { ..., {1, 2, 3, -1}, ...},
>   { ..., {4, -1}, ...},
>   ...
>};
>
>It doesn't work. I know I could declare the lists of integers as separate
>variables and use the address, but it would be much harder to see what data
>belongs where, and I would have to use *lots* of variable names.

This is pretty much the only way to do it.

>I could also write 'int int_list[MAX];' instead of 'int *int_list;', but in
>that case lots of space would be wasted if there is big variance in length.

Right.

>Is there any simple way to do it?

Use a preprocessor (not `cpp'); have it emit something like

	int L0[] = { 1, 2, 3, -1 };
	int L1[] = { 4, -1 };
	...
	struct ... list_of_structs[] = {
		{ ..., L0, ... },
		{ ..., L1, ... },
		...
	};

where each Li is the array to which list_of_structs[i] should point.
-- 
In-Real-Life: Chris Torek, Lawrence Berkeley Lab CSE/EE (+1 415 486 5427)
Berkeley, CA		Domain:	torek at ee.lbl.gov



More information about the Comp.lang.c mailing list