在C99中,为什么将变量声明p
为指向数组的指针需要在将其作为参数传递给具有数组类型参数的函数之前进行强制转换,但是将变量声明p
为void指针然后将其强制转换为指向数组的指针可以作为指向数组的指针传递给同一个函数?
#include <stdio.h>
int arreglo(int locArr[])
{
locArr[0]=1;
printf("el arreglo es : %i\n",locArr[0]);
return 0;
}
int main()
{
/* Declare a pointer p to array */
int (*p)[];
int manArr[10];
p=&manArr; /* assign the adress of manArr as case below */
/* Here passing pointer p is not allowed as expected,
since our function has int* as argument */
/* so I need to do a casting */
arreglo((int*)p);
}
/* **But in this other main function**: */
int main()
{
/* Declare a void pointer */
void *p=NULL;
/* Do a casting from p to void to p to array */
p=(int (*)[])p;
int manArr[10];
p=&manArr; /* assing the adress of the array manArr as in above case */
/* Now given the pointer to array as parameter to function WORKS¡¡,
why?. As before the function expects int* as argument not
a pointer to an array */
arreglo(p);
}