59 lines
1.5 KiB
C
59 lines
1.5 KiB
C
/* bigInt.c
|
|
*
|
|
* Demonstrates use of passing an array to a function.
|
|
*
|
|
* Also shows why sizeof() is not a reliable way to discover array size. It
|
|
* *only* works correctly inside the function it's declared in. When passed
|
|
* to a function, the *address* of teh begining of the array is passed and so
|
|
* sizeof() returns the sizeof an address. Here's the compiler output
|
|
* (I ignored the compile warnings to run it)
|
|
*
|
|
* wightman% gcc bigInt.c
|
|
* bigInt.c:32:49: warning: sizeof on array function parameter will return size of
|
|
* 'int *' instead of 'int []' [-Wsizeof-array-argument]
|
|
* printf("bigInt: sizeof(a): %lu bytes\n", sizeof(arr));
|
|
* ^
|
|
* bigInt.c:29:16: note: declared here
|
|
* int bigInt(int arr[], int iNarr){
|
|
* ^
|
|
* 1 warning generated.
|
|
*/
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#define N 5
|
|
|
|
int bigInt(int arr[], int iNarr);
|
|
|
|
int main()
|
|
{
|
|
int i;
|
|
int iErr;
|
|
int a[5];
|
|
|
|
// sizeof() return a long unsigned int (%lu)
|
|
printf("main: sizeof(a): %lu bytes\n", sizeof(a));
|
|
|
|
printf("enter some integers,followed by ^D on blank line: ");
|
|
i = 0;
|
|
iErr = 1;
|
|
while( iErr != 0 && i < N ){
|
|
iErr = scanf("%d",&a[i]);
|
|
i++;
|
|
}
|
|
|
|
printf("Biggest value: %d\n", bigInt(a,N));
|
|
|
|
return EXIT_SUCCESS;
|
|
}
|
|
|
|
int bigInt(int arr[], int iNarr){
|
|
int i;
|
|
int big = arr[0];
|
|
printf("bigInt: sizeof(a): %lu bytes\n", sizeof(arr));
|
|
for(i=1; i<iNarr; i++){
|
|
if(arr[i] > big)
|
|
big = arr[i];
|
|
}
|
|
return big;
|
|
}
|