| arrays/array-limitations-2a.c - Et array der overføres som input parameter - compilerer ikke. | Lektion 9 - slide 4 : 30 Program 4 |
#include <stdio.h>
#define TABLE_SIZE 11
void f (const double p[TABLE_SIZE]){
int i;
for(i = 0; i < TABLE_SIZE; i++)
p[i] = p[i] * 2; // Compile time error:
// Assignment of read only location
}
int main(void) {
double a[TABLE_SIZE];
int i;
for(i = 0; i < TABLE_SIZE; i++)
a[i] = 2*i;
for(i = 0; i < TABLE_SIZE; i++)
printf("Before calling f: a[%d] = %f\n", i, a[i]);
printf("\n");
f(a); // The array a is passed as a pointer.
for(i = 0; i < TABLE_SIZE; i++)
printf("After calling f: a[%d] = %f\n", i, a[i]);
return 0;
}