Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!ames!oliveb!amiga!cbmvax!kevin From: kevin@cbmvax.UUCP (Kevin Klop) Newsgroups: comp.sys.amiga Subject: Re: Struggling through the "C" Message-ID: <6542@cbmvax.UUCP> Date: 7 Apr 89 22:09:56 GMT References: <2570@ssc-vax.UUCP> <231@nlgvax.UUCP> Reply-To: kevin@cbmvax.UUCP (Kevin Klop) Organization: Commodore Technology, West Chester, PA Lines: 91 In article <231@nlgvax.UUCP> hans@nlgvax.UUCP (Hans Zuidam) writes: >>In article <97113@sun.Eng.Sun.COM>, Chuck McManis writes: >>>char *a; /* This is a pointer to a string */ >>>char a[80]; /* this is a pointer to a string with 80 bytes allocated */ > >In article <2570@ssc-vax.UUCP> dmg@ssc-vax.UUCP (David Geary) writes: >> No! char a[80] is not a pointer at all. It is an array... > [Several K&R quotations deleted] >So: > 'char *a' is *identical* to 'char a[80]' *except* for the > fact that in the second definition 'a' is a *constant* > pointer. That is: you cannot assign a value to 'a'. Actually, there is a difference between 'a' in the following two declarations: char a[80]; char *a; To demonstrate, though, let us look at the following statement: *a = '\3'; in assembly code, this would translate to (I have made the assumption that both declarations are GLOBAL in scope rather than allocated on the local stack): ; For the declaration "char a[80];" move.b #3,a ; move a byte into location referenced by 'a' ; For the declaration "char *a;" movea.l a,A0 ; get the pointer from memory move.b #3,(A0) ; and place a 3 into where it points you will notice that for the declaration "char *a;" we have to do the equivalent of a store indirect through a, whereas in the declaration "char a[80];" we store to the location "a". Note that this is hand coded assembly equivalences, and your compiler mileage may vary... :^) Thus, there is a very real difference between the two declarations. Usually there is no problem treating the two of them identically since the compiler takes care of it for you. However, since the identifier "a" has to be referenced differently for the two declarations, the following scenario can cause one many problems: Module 1: char a[80] main() { /* I don't know, put some of your favorite code here...*/ } Module 2: extern char *a; anotherfunction() { *a = '\0'; /* Insure a 0 length string */ . . . } the code generated in Module 2, "anotherfunction" is going to incorrectly reference the array. In fact, what it is going to do is concatenate the first four entried from the array, form those into a pointer, and store a byte length 0 whereever the concatenation points to. This is a BAD thing to do, in general. [More deleted here] >-- >Hans Zuidam E-Mail: hans@pcg.philips.nl -- Kevin -- Kevin Klop {uunet|rutgers|amiga}!cbmvax!kevin Commodore-Amiga, Inc. The number, 111-111-1111 has been changed. The new number is: 134-253-2452-243556-678893-3567875645434-4456789432576-385972