TIP: Click on subject to list as thread! ANSI
echo: cis.tandy.coco
to: Pete Lyall 76703,4230 (X)
from: Jeff Dege 76426,211
date: 1990-05-04 12:45:04
subject: #3268-#C arrays

#: 3270 S10/Tandy CoCo
    04-May-90  12:45:04
Sb: #3268-#C arrays
Fm: Jeff Dege 76426,211
To: Pete Lyall 76703,4230 (X)

   I said a "int foo[2][6];" declared a POINTER to an array of 6 ints. "int
foo[6];" declares a pointer to an int (it isn't a convention, it is the truth. 
Array notation is a convenient substitute for pointer arithmentic, no more.) 
Whether there is any memory allocated depends upon whether this is a variable
or a parameter declaration.  If "int foo[2][6];" is a variable declaration, foo
becomes a pointer to an array of 6 ints, initialized to a memory area large
enough to hold two such arrays.  Since pointer arithmetic is scaled by the size
of the base type, *foo is the first array of 6 ints, *foo+1 is the second array
of 6 ints. **foo points to the first int in the first array, and **foo+1 points
the the second int in the first array. &foo, foo, and *foo have the same VALUE,
but different type.  Assuming that an int is two bytes, and the base address of
foo is 0x100, &foo+1 = 0x124, foo+1 = 0x12, and *foo+1 = 0x102 (**foo+1 is an
int, not a pointer ;). If you add in the fact that a VARIABLE array is an
r-value, and can't be changed, while a parameter array declaration is a
pointer, and has no such limitation, you have a complete understanding of how
arrays work in C.


   In any case, the whole system is designed so that you don't HAVE to be aware
of how arrays equate to pointers in order for them to work.  You don't HAVE to
understand why X[5] == 5[X].  You can use arrays just like you would in Pascal,
subject to the limitations that indices always start at 0, and arrays are
always passed by reference.  I always worry that these discussions will
completely confuse the newcomers




There is 1 Reply.

SOURCE: compuserve via textfiles.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.