C has no keyword "array" and since C has no classes there is no Array class as in Java. C does, however, have an array type. C arrays are contiguous storage of homogeneous values that are addressable by index (starting at 0). When you declare an array, you are guaranteed that all the elements are of the same type and that they are stored in consecutive memory addresses. The value of a variable that is an array is the address of the first element (element 0) of the array. Thus,
int a[10]; int *pa; pa = a; /* is the same as saying pa = &a[0] */
BUT while pa is a variable (an l-value), an array name is not, it's a synonym for the location of the first element. Thus,
int fred; int a[10], b[10]; a[8] = fred; /* OK */ a[0] = a[1]; /* OK */ a[i] = a[i+1]; /* OK */ a[0] = b[0]; /* OK */ a = b; /* ILLEGAL! */
But be careful! Consider the following code snippet (available as arraysize.c):
void printSize(int arr[]) { printf("%d\n", (int)sizeof arr); /* same as sizeof(int *) */ } int main(int argc, char *argv[]) { double *p; double arr[42]; printf("%d\n", (int)sizeof p); printf("%d\n", (int)sizeof &arr[0]); /* addr of 1st elem */ printf("%d\n", (int)sizeof arr); /* sizeof the entire block (array) */ printf("%d\n", (int)(sizeof arr/sizeof arr[0])); /* number of elements */ printSize(arr); return 0; }
The first two will print the size (in bytes) of a pointer on your system (probably 4). The third will NOT be 4 as it will literally print the size of the entire array (336) since it is declared (dimensioned) in this function. Given that that's the case, the next value printed will be the number of elements (the size of the entire structure divided by the size of one (arbitrarily-chosen) element. And the last value printed, from within the function call, will NOT be 336; it will be 4, the size of a pointer (which is what it is sent!).
We then examined
Arrays can be declared and initialized in the same statement just by supplying a list of values in braces. In this case, you can use empty brackets, [ ], since the compiler will parse the list and calculate the appropriate dimension.
Arrays can be declared and dimensioned but left uninitialized. In this case, a compile-time constant (usually via a #define statement) must be inside the braces because arrays are created at compile-time so the compiler does not have access yet to run-time values.
When arrays are passed as parameters, you just pass the name as in Java and get the address of the array as the value received (because the array name is a synonym for the address of first item in the array). Note that the receiving formal parameter doesn't have to have a number in the brackets. That's OK - you're given where to start, but you have to determine where to stop somehow (since there's no .length instance variable as in Java!).
Look at the last part of the code in the main and notice the following: when we scanf(..) for c[0] and d[0], we used the arrays' names as parameters to pass the addresses of c[0] and d[0]. This works because the values of the symbols c and d are the address of c[0] and d[0], respectively. We will come back and look in more gory detail at the semantics of arrays, pointers and the [ ] operator when we cover pointer arithmetic a little later in the course.
In the very last line of main when we print the values of c[0] and d[0], what syntax could we have used instead of c[0] and d[0] to supply printf() with the values of those elements?