Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!cornell!uw-beaver!ssc-vax!dmg From: dmg@ssc-vax.UUCP (David Geary) Newsgroups: comp.sys.amiga Subject: Re: Struggling through the "C" Message-ID: <2606@ssc-vax.UUCP> Date: 14 Apr 89 22:19:37 GMT Organization: Boeing Aerospace Corp., Seattle WA Lines: 99 In article <231@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... | |Please check your K&R before responding: | | K&R 7.1, pg. 185: | An identifier is a primary expression, provided it has been | suitably declared as discussed below. If the type of the identifier | is "array of ...", then the value of the identifier-expression is a ^^^^^^^^ Please *understand* your K&R before responding ;-) Of course, this is true - the *value* of the identifier-expression is a pointer to the first object in the array. What this means is: main() { char a[80]; /* an ARRAY - NOT a pointer */ DoSomething(a); /* here, the value of a is equivalent to &a[0], which is a pointer to the first object in the array a. This means we could have written: DoSomething(&a[0]); and gotten the same result. */ } | pointer to the first object in the array, and the type of the | expression is "pointer to ...". | |and | | K&R 7.1, pg. 186: | The expression E1[E2] is identical (by definition) to *((E1)+(E2)). Yes, this is true. | |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'. No! (no no no ;-) - This is a common misconception that many of my students in my introductory C class make. char *a is NOT identical to char a[80]; While it's true that in char a[80], the *value* of a is a constant pointer, and in char *a, a is a variable pointer, there is more difference than that: char *a; This gives you a pointer, period. a can hold the address where a char sits in memory: --------- | |-----------> --------- char a[80]; This gives you an actual array of characters, there is no pointer involved: --------- | a[0] | --------- | a[1] | --------- | a[2] | --------- . . . If you think that char *a and char a[80] are *identical* , *except* for the fact that the second definition...(as stated above), try this: main() { char a[80], *p; strcpy(a, "I can fit in array a, because there is memory allocated for me"); strcpy(p, "This is nothing but trouble"); } Notice that the second strcpy is going to cause you severe headaches (especially on an Amiga). -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~ David Geary, Boeing Aerospace, Seattle ~ ~ "I wish I lived where it *only* rains 364 days a year" ~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~