I'm getting the following warning:
incompatible pointer types 'void**' and 'int* [2]'
.
When I try to compile the following code:
#include <stdlib.h>
void func1(void *arr[]) { }
int main() {
int *arr[2];
for (int i = 0; i < 5; i++) {
arr[i] = (int*)malloc(sizeof(int));
*(arr[i]) = 5;
}
func1(arr);
}
Now, it works when I cast arr with (void**)
, and I couldn't find a reason for that. Furthermore I found that I also need to cast in the following code:
#include <stdlib.h>
void func1(void **arr) { }
int main() {
int **arr;
int i[] = { 1, 2 };
int j[] = { 3, 4 };
*arr = i;
*(arr+1) = j;
func1(arr); //Doesn't compile unless I use (void*) or (void**) casting
}
I know that if a function's parameter is a pointer to void
we can pass to it whatever pointer we want without casting because all pointers are of the same size then why can't I pass a pointer to a pointer the same way?