| Part 1: Examples from Above |
#include <stdio.h>
main() {
int array [10];
double rreal[10];
int i;
for (i = 0; i < 10; i++) {
array[i] = i;
rreal[i] = i*0.1;
printf("%d ", &array[i]);
printf("%d\n", &rreal[i]);
}
printf("That's all!\n");
}
|
/* Illustrates the difference between call-by-value
and call-by-reference. */
#include <stdio.h>
void call_by_value(int y) {
y = 1;
}
void call_by_reference(int *y) {
*y = 1;
}
main() {
int x = 5;
printf("original: x = %d\n", x);
call_by_value(x);
printf("should be same as before: x = %d\n", x);
call_by_reference(&x);
printf("should have changed: x = %d\n", x);
}
|
/* Illustrates the passing of arrays to functions. */
#include <stdio.h>
#define SIZE 5
int sum(int *data, int num_items) {
int total, i;
for (i = total = 0; i < num_items; i++)
total = total + *(data + i); /* Try both versions of */
/* this line (see Arrays */
return total; /* as Args, above). */
}
main() {
int values[SIZE], i;
for (i = 0; i < SIZE; i++)
values[i] = i;
printf("total = %d\n", sum(values, SIZE));
}
|
| Part 2: Run-time Errors |
The remaining examples are mainly to illustrate run-time errors, though there also compile-time errors.
| ...previous | up (conts) | next... |