Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!utgpu!water!watmath!clyde!rutgers!seismo!mcnc!ece-csc!ncrcae!ncr-sd!hp-sdd!hplabs!hpcea!hpda!hpihoah!hpisod2!decot From: decot@hpisod2.UUCP Newsgroups: comp.lang.c Subject: Re: static char (*b)[6]; /* an unusual declaration */ Message-ID: <2550018@hpisod2.HP.COM> Date: Fri, 12-Jun-87 20:05:17 EDT Article-I.D.: hpisod2.2550018 Posted: Fri Jun 12 20:05:17 1987 Date-Received: Sun, 14-Jun-87 21:59:01 EDT References: <761@bsu-cs.UUCP> Organization: Hewlett Packard, Cupertino Lines: 44 > What is the meaning of the following declaration? > > static char (*b)[6]; b has static storage and is a pointer to a (pseudo-named) array of 6 characters (not to the first character of an array, mind you, but to something which may be used in any context where a name of an array is allowed). Initially, b is a null-pointer. sizeof(*b) is 6*sizeof(char). For instance, ++b increases the pointer address by six characters. *b is equivalent to the name of a particular array, and since it has no initial value, it never will. > The compiler and lint checker used by the magazine columnist seem to > treat the declaration of b as equivalent to char ** but the following > strange things happen. Not really, since sizeof(*b) is not sizeof(char *), although (*b)[0] is a character. > main() > { > static char (*b)[6]; /* an unusual declaration */ > static char a[] = "Hello"; > char *c = a; > char **d = &c; > b = d; /* So b == d ... Watch. */ This statement should have encountered an error, since b and d are not of compatible types. > printf ("%s\n", *d); /* prints "Hello" */ > printf ("%s\n", *b); /* prints 0 */ > } > Other code fragments omitted. The columnist continues, "...This means > that *b is a constant and while (b) can be modified, *b never will > change its original value. Thus, setting b = d does not mean that > *b == *d. Also, while *b is treated as a constant (unchangeable), the > value of b can vary." This is accurate. Dave Decot hpda!decot