p
C99 では、変数を配列型の引数を持つ関数にパラメーターとして渡す前に、配列へのポインターとして変数を宣言する必要があるのはなぜですか。変数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);
}