jhpb@granjon.UUCP (Joseph H. Buehler) (09/17/90)
I have some structures that refer to each other, that I want to be static. The compiler doesn't like the following because it doesn't know what y is when it's initializing x: static struct xtag x = { &y; }; static struct ytag y = { &x; }; Does ANSI C have any way to do this? For now, I had to drop the static and put an extern at the top of the file: extern struct ytag y; struct xtag x = { &y; }; struct ytag y = { &x; }; Which is OK, I guess, but the structures really don't need global accessibility. -- Joe Buehler
scjones@thor.UUCP (Larry Jones) (09/21/90)
In article <1287@granjon.UUCP>, jhpb@granjon.UUCP (Joseph H. Buehler) writes: > I have some structures that refer to each other, that I want to be > static. The compiler doesn't like the following because it doesn't know > what y is when it's initializing x: > > static struct xtag x = {&y}; > static struct ytag y = {&x}; > > Does ANSI C have any way to do this? static struct ytag y; /* tentative definition */ static struct xtag x = {&y}; static struct ytag y = {&x}; /* actual definition */ > For now, I had to drop the static and put an extern at the top of the file Which is what you'll have to do for most non-ANSI compilers. ---- Larry Jones UUCP: uunet!sdrc!thor!scjones SDRC scjones@thor.UUCP 2000 Eastman Dr. BIX: ltl Milford, OH 45150-2789 AT&T: (513) 576-2070 I'm not a vegetarian! I'm a dessertarian. -- Calvin
eyal@echo.canberra.edu.au (Eyal Lebedinsky) (09/22/90)
In article <1287@granjon.UUCP> jhpb@granjon.att.com (01000-JH Buehler(BLU123)) writes: >I have some structures that refer to each other, that I want to be >static. The compiler doesn't like the following because it doesn't know >what y is when it's initializing x: > > static struct xtag x = { > &y; > }; > > static struct ytag y = { > &x; > }; > >Does ANSI C have any way to do this? For now, I had to drop the static ANSI asks you to add a line: extern struct ytag y; before the first definition. Note that 'extern' does not mean that y is 'external to this file' but that 'it is defined elsewhere'. The 'static' definition later will complete the meaning. Some one-pass compilers will have lots of trouble with this. ANSI 3.7.2 explaines this 'tentative definition' stuff. It says that saying 'static struct ytag y;' should work because this is tentative and the later initialized definition is real. I know that some compilers don't like this either. This is generaly a sticky area. NOTE that I said to leave the rest of your example as is, with the 'static's. >-- >Joe Buehler -- Regards Eyal
brnstnd@kramden.acf.nyu.edu (Dan Bernstein) (09/22/90)
In article <186@thor.UUCP> scjones@thor.UUCP (Larry Jones) writes: > In article <1287@granjon.UUCP>, jhpb@granjon.UUCP (Joseph H. Buehler) writes: > > I have some structures that refer to each other, that I want to be > > static. The compiler doesn't like the following because it doesn't know > > what y is when it's initializing x: > > static struct xtag x = {&y}; > > static struct ytag y = {&x}; A different approach should work under any C compiler, though it may not be implemented efficiently. It also has the usual syntactic problems of macros. static struct { struct xtag x; struct ytag y; } shmoe = { &(shmoe.y), &(shmoe.x) } ; #define x (shmoe.x) #define y (shmoe.y) Caveat: This is untested. But the idea is useful to remember. ---Dan
karl@haddock.ima.isc.com (Karl Heuer) (09/25/90)
In article <1990Sep21.230632.19238@csc.canberra.edu.au> eyal@echo.canberra.edu.au (Eyal Lebedinsky) writes: >ANSI asks you to add a line: `extern struct ytag y;' before the first >definition. Note that 'extern' does not mean that y is 'external to this >file' but that 'it is defined elsewhere'. True, but it also means that the symbol is expected to have external linkage, unless it's already been declared otherwise (see 3.1.2). You have to use the `static' keyword instead. (Note that this is how to do it in ANSI C. There is no sure way to do it in Classic C.) Karl W. Z. Heuer (karl@kelp.ima.isc.com or ima!kelp!karl), The Walking Lint