Path: utzoo!utgpu!jarvis.csri.toronto.edu!rutgers!apple!ames!ncar!tank!mimsy!chris From: chris@mimsy.UUCP (Chris Torek) Newsgroups: comp.lang.c Subject: Re: Array intialization Keywords: array of pointers Message-ID: <17862@mimsy.UUCP> Date: 3 Jun 89 04:30:14 GMT References: <3420@ihuxv.ATT.COM> Organization: U of Maryland, Dept. of Computer Science, Coll. Pk., MD 20742 Lines: 47 In article <3420@ihuxv.ATT.COM> bareta@ihuxv.ATT.COM (Benyukhis) writes: >The following declaration is illegal. Why?????? > >char *a = "string1"; >char *b = "string2"; >char *c = "string3"; > >char *g[] = { a, b, c }; It is illegal because initialisers must be constant expressions, and, by fiat%, initialised variables are not constant expressions---the situation is comparable to int a = 1, b = 2, c = 3; int g[] = { a, b, c }; >What if the compiler was 3 pass one? The number of passes in any implementation is irrelevant to the language definition itself (which is where one must turn for questions of legality). You are probably confusing this with char a[] = "string1", b[] = "string2", c[] = "string3"; char *g[] = { a, b, c }; which *is* legal because the address of a global or static variable *is* included in the set of things that make up a `constant expression'. The latter is the same as char *g[] = { &a[0], &b[0], &c[0] }; which is comparable to int a = 1, b = 2, c = 3; int *g[] = { &a, &b, &c }; ----- % All matters of definition are by fiat :-) . Actually, the reasoning behind this is that for initialised variables, the compiler can emit the name of the variable and its value, then forget the latter, but the linker must be able to resolve addresses anyway, so allowing object addresses as constant expressions does not make the system's overall job any harder. -- In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163) Domain: chris@mimsy.umd.edu Path: uunet!mimsy!chris