This is an exercise from the book C Programming: A modern approach by K.N King
"Assume that the following array contains a week's worth of hourly temperature readings, with each row containing the readings for one day:
int temperatures[7][24];
Write a statement that uses the search function to search the entire temperatures array
for the value 32."
My code below includes my search function from a previous exercise, and main() where I call the function. Search simply processes an array and checks if any element is equal to a supplied argument "key". The solution appears to compile and execute the expected output, it prints "32 was found", or prints nothing if I comment out the assignment temperatures[5][22] = 32;
.
Notice how the function takes a 1d array but the exercise is asking to process a 2d array.
I initially tried this solution without the explicit type cast (int*) in the function call, and got this when compiling (I reformatted it a bit):
1 compiler warning: "passing argument 1 of search from incompatible pointer type"
1 note: "expected 'const int *' but argument is of type 'int * [24]'"
Why do the warnings/note appear? If there is an incompatibility, why are they not errors? The type cast to int* removes any compiling issues, but is the solution actually correct/safe or bad C understanding/practice on my part? I understand that the function expects int* but that the 2d array decays to a pointer of type int * [24] without the type cast. Yet the code works in either case, albeit I just had one test case. How could I modify my solution logic to avoid this issue at all? The search function should stay the same though.
#include <stdbool.h>
#include <stdio.h>
bool search(const int a[], int n, int key);
int main(void){
int temperatures[7][24] = {0};
temperatures[5][22] = 32;
bool has32 = search((int*)temperatures, 7 * 24, 32);
if(has32)
printf("32 was found");
return 0;
}
bool search(const int a[], int n, int key){
const int *p;
for(p = a; p < a + n; p++)
if(*p == key)
return true;
return false;
}