Is there any reason why:
void function_foo(){
int k[8];
function_math(k, 8);
}
void function_math(int *k, int i){
printf("value: %d", k[i]);
}
The main execute function_foo()
;
The output will be 1? There's no initialization for elements of matrix k. Maybe something with the length of int in memory?
I am new to C concepts, the pointers and everything.
It is undefined behaviour to evaluate k[8]
, since k
only has 8 elements, not 9.
There is little point arguing about the consequences of undefined behaviour. Anything could happen. Your program is not well-formed.
(Note that it would even be undefined behaviour to evaluate k[0]
, ..., k[7]
, since they are uninitialized. You have to write to them first, or initialize the array, such as int k[8] = { 1, 2 };
.)