Below is the code snippet. I want to know if line no. 17 typecasting is valid and proper in c?
#include <stdio.h>
typedef int twoInts[2];
void print(twoInts *twoIntsPtr);
void intermediate (twoInts twoIntsAppearsByValue);
int main () {
twoInts a;
a[0] = 0;
a[1] = 1;
print(&a);
intermediate(a);
return 0;
}
void intermediate(twoInts b) {
print((int(*)[])b); // <<< line no. 17 <<<
}
void print(twoInts *c){
printf("%d\n%d\n", (*c)[0], (*c)[1]);
}
Also, when i change the definition intermediate to
void intermediate(twoInts b) {
print(&b);
}
I am getting below warnings while compiling and o/p is not proper.
1.c:17:11: warning: passing argument 1 of print from incompatible pointer type
print(&b);
^
1.c:5:6: note: expected int (*)[2] but argument is of type int **
void print(twoInts *twoIntsPtr);
As per my understanding, array is decaying to pointer to int in funtion argument. What is the exact reason?
void print(twoInts *twoIntsPtr);is identical to:void print(int (*twoIntsPtr)[2]);Or in other words, don't hide arrays and pointers behind typedefs.int*pointers without the need to obfuscate the program to oblivion.print((twoInts *)&b);should do.