next up previous
Next: September 25 Up: September 20 Previous: Logic


Arrays

The commonest aggregate type is an array:

int intArray[5], *ip;

intArray[2]= 1073;
*(intArray + 3)= 1074;
ip= &intArray[2];
... ip[1] == intArray[3] <---- this expression evaluates to 1
The declaration reserves 5 $ \times$ sizeof(int) contiguous bytes of memory. The identifier intArray, without the subscript, contains the address of the zeroeth element. Pointers and arrays may be manipulated in similar ways. Important difference: address corresponding to an array may not be changed:
char word[3];

word= "hi"; <--- strings are char arrays terminated by '\0'
                 the constant array on the RHS cannot be assigned
                 to word
When you pass an array to a function, it sees a pointer, and it doesn't know the size of the array.
int arrayFunc(int *intArray) <------- same as int intArray[])
{
    int k= 2;

    return intArray[k]; <-------- same as k[intArray] --- WHY?
}

Another important aggregate type is struct. You can read some of Alan Rosenthal's notes on struct



Danny Heap 2002-12-16