EXERCISES ON POINTERS AND ARRAYS
1. Assume int x[5] = {0, 1, 2, 3, 4}. What are the values of x after calling
swap3(x, 1, 4)?
void swap3(int a[], int i, int j) {
int t;
t = a[i]; a[i] = a[j]; a[j] = t;
}
2. Assume int x[5] = {0, 1, 2, 3, 4}. What are the values of x after calling
swap2(x+1, x+4)?
3. Assume int x[5] = {0, 1, 2, 3, 4}. What does print3(&x[0]) print?
print3(&x[2])? print3(&x[4])?
void print3(int x[]) {
int i;
for (i = 0; i < 3; i++)
printf("%d ", x[i]);
}
4. When we pass an array to a function in C, it passes a pointer to array
element 0 instead. Why doesn't C just create a new local copy of
the array, as it does with integers?
5. What is the difference between the following two function:
int middle1(int a[], int n) { int middle2(int *a, int n) {
return a[n/2]; return a[n/2];
} }