September 1998
Intermediate to advanced
848 pages
20h 13m
English
Suppose you want to write a function that operates on an array. For instance, suppose you want a function that returns the sum of the elements of an array. Listing 10.7 shows a program that does this. To point out an interesting fact about array arguments, the program also prints the size of the relevant arrays. (Recall that some pre-ANSI compilers require %ld for printing sizeof quantities.)
/* sum_arr1.c -- sums the elements of an array */ #include <stdio.h> #define SIZE 10 long sum(int ar[], int n); int main(void) { int marbles[SIZE] = {20,10,5,39,4,16,19,26,31,20}; long answer; answer = sum(marbles, SIZE); printf("The total number of marbles is %ld.\n", answer); printf("The ... |
Read now
Unlock full access