Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.1 6/24/83; site allegra.UUCP Path: utzoo!linus!decvax!harpo!floyd!whuxlb!pyuxll!eisx!npoiv!npois!hogpc!houxm!hocda!spanky!burl!sb1!ll1!otuxa!we13!ihnp4!cbosgd!mhuxi!mhuxt!eagle!allegra!alan From: alan@allegra.UUCP Newsgroups: net.lang.c Subject: Re: mixing pointers and arrays Message-ID: <1705@allegra.UUCP> Date: Sun, 31-Jul-83 10:25:58 EDT Article-I.D.: allegra.1705 Posted: Sun Jul 31 10:25:58 1983 Date-Received: Wed, 3-Aug-83 17:57:16 EDT References: <2332@csu-cs.UUCP>, <919@rlgvax.UUCP> Organization: Bell Labs, Murray Hill Lines: 64 Consider the following fragments: --- file 1 --- extern char *foo; func() { printf("%c", *foo); } --- file 2 --- char foo[SIZE]; --- The declaration extern char *foo; says that foo is a variable holding the address of a character (in this case, the first character of an array). This is not correct. The variable foo doesn't refer to a pointer to an array, but to an array itself. On the other hand, main() { char foo[SIZE]; func(foo); } func(foo) char *foo; { printf("%c", *foo); } will work just fine, although it would have been clearer to declare foo as char foo[]; in func(). The key here is that in the first example, the foo in file one is the same variable as the foo in file two, while in the second example, we have two different foo's. By saying func(foo); we create a character pointer. In func(), foo now refers to a location on the stack which holds the address of the first character of the array known by the name foo in main(). Like so many other blunders, this is all done in the name of efficiency. As far as I'm concerned, it's one of the ugliest parts of the C language. Alan Driscoll BTL, Murray Hill