
CS485G Spring 2015 27
27 Arrays
1. Lecture 15, 2/23/2015
2. C declaration: T myArray[L], where T is some type and L is the
number of elements (the first is number 0).
3. Contiguously allocated region of L × sizeof(T ) bytes.
4. Examples
declaration length
char string[12] 12
int val[5] 20
double a[3] 24
char
*
p[3] 12 (on x86)
char
*
p[3] 24 (on x86 64)
5. C syntax, given int val[5]; stored starting at location x, contain-
ing 1, 2, 3, 4, 5.
expression type value
val[4] int 3
val int * x
val+1 int * x+4
&val[2] int * x+8
val[4] int 5
val[5] int garbage
*
(val+1) int 2
val + i int * x+4i
6. Using the same type for several arrays
1 #define ZLEN 5
2 typedef int myArrayType[ZLEN];
3 myArrayType cmu = { 1, 5, 2, 1, 3 };
4 myArrayType mit = { 0, 2, 1, 3, 9 };
5 myArrayType uky = { 9, 4, 7, 2, 0 };
It’s possible, but not guaranteed, that the three arrays are consecutive
in memory.
7. Simple example: return uky[dig];
# assume d = uky
# assume a = dig
movl (%edx,%eax,4),%eax # a = uky[dig]
Komentarze do niniejszej Instrukcji