C将int数组指针作为参数传递给函数

我想将B int数组指针传递给func函数,并能够从那里更改它,然后查看main函数中的更改

#include  int func(int *B[10]){ } int main(void){ int *B[10]; func(&B); return 0; } 

上面的代码给了我一些错误:

 In function 'main':| warning: passing argument 1 of 'func' from incompatible pointer type [enabled by default]| note: expected 'int **' but argument is of type 'int * (*)[10]'| 

编辑:新代码:

 #include  int func(int *B){ *B[0] = 5; } int main(void){ int B[10] = {NULL}; printf("b[0] = %d\n\n", B[0]); func(B); printf("b[0] = %d\n\n", B[0]); return 0; } 

现在我得到这些错误:

 ||In function 'func':| |4|error: invalid type argument of unary '*' (have 'int')| ||In function 'main':| |9|warning: initialization makes integer from pointer without a cast [enabled by default]| |9|warning: (near initialization for 'B[0]') [enabled by default]| ||=== Build finished: 1 errors, 2 warnings ===| 

在您的新代码中,

 int func(int *B){ *B[0] = 5; } 

B是指向int的指针,因此B[0]是一个int ,你不能取消引用int 。 只需删除*

 int func(int *B){ B[0] = 5; } 

它的工作原理。

在初始化中

 int B[10] = {NULL}; 

你正在使用void*NULL )初始化一个int 。 由于存在从void*int的有效转换,但是它不太合适,因为转换是实现定义的,并且通常表示程序员的错误,因此编译器会对此发出警告。

 int B[10] = {0}; 

是0初始化int[10]的正确方法。

也许你正试图这样做?

 #include  int func(int * B){ /* B + OFFSET = 5 () You are pointing to the same region as B[OFFSET] */ *(B + 2) = 5; } int main(void) { int B[10]; func(B); /* Let's say you edited only 2 and you want to show it. */ printf("b[0] = %d\n\n", B[2]); return 0; } 

如果你真的想传递一个数组指针,那就是

 #include  int func(int (*B)[10]){ // ptr to array of 10 ints. (*B)[0] = 5; // note, *B[0] means *(B[0]) //B[0][0] = 5; // same, but could be misleading here; see below. } int main(void){ int B[10] = {0}; // not NULL, which is for pointers. printf("b[0] = %d\n\n", B[0]); func(&B); // &B is ptr to arry of 10 ints. printf("b[0] = %d\n\n", B[0]); return 0; } 

但正如其他答案中所提到的,做到这一点并不常见。 通常只有当你想传递一个2d数组时才会传递指向数组的指针,它突然看起来更加清晰,如下所示。 实际上,2D数组作为指向其第一行的指针传递。

 int func( int B[5][10] ) // this func is actually the same as the one above! { B[0][0] = 5; } int main(void){ int Ar2D[5][10]; func(Ar2D); // same as func( &Ar2D[0] ) } 

func的参数可以声明为int B[5][10]int B[][10]int (*B)[10] ,都等同于参数类型。

附录:你可以从一个函数返回一个指向数组的指针,但声明该函数的语法非常笨拙,该类型的[10]部分必须在参数列表之后:

 int MyArr[5][10]; int MyRow[10]; int (*select_myarr_row( int i ))[10] { // yes, really return (i>=0 && i<5)? &MyArr[i] : &MyRow; } 

这通常如下所述,以避免眼睛疲劳:

 typedef int (*pa10int)[10]; pa10int select_myarr_row( int i ) { return (i>=0 && i<5)? &MyArr[i] : &MyRow; } 

在新的代码分配应该是,

 B[0] = 5 

在func(B)中,您只是传递指向数组B的指针的地址。您可以将func()更改为B [i]或*(B + i)。 其中i是数组的索引。

在宣言中说的第一个代码中,

 int *B[10] 

说B是一个包含10个元素的数组,每个元素都是指向int的指针。 也就是说,B [i]是一个int指针,而* B [i]是它指向第i个保存文本行的第一个整数的整数。

 main() { int *arr[5]; int i=31, j=5, k=19, l=71, m; arr[0]=&i; arr[1]=&j; arr[2]=&k; arr[3]=&l; arr[4]=&m; for(m=0; m<=4; m++) { printf("%d",*(arr[m])); } return 0; } 

在函数声明中,您必须键入为

 VOID FUN(INT *a[]); /*HERE YOU CAN TAKE ANY FUNCTION RETURN TYPE HERE I CAN TAKE VOID AS THE FUNCTION RETURN TYPE FOR THE FUNCTION FUN*/ //IN THE FUNCTION HEADER WE CAN WRITE AS FOLLOWS void fun(int *a[]) //in the function body we can use as a[i]=var