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

fxu*_*ser 27 c arrays parameters pointers function

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

#include <stdio.h>

int func(int *B[10]){

}

int main(void){

    int *B[10];

    func(&B);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

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

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]'|
Run Code Online (Sandbox Code Playgroud)

编辑:新代码:

#include <stdio.h>

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;
}
Run Code Online (Sandbox Code Playgroud)

现在我得到这些错误:

||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 ===|
Run Code Online (Sandbox Code Playgroud)

Dan*_*her 35

在您的新代码中,

int func(int *B){
    *B[0] = 5;
}
Run Code Online (Sandbox Code Playgroud)

B是一个指针int,因此B[0]是一个int,你不能解除引用int.只需删除*,

int func(int *B){
    B[0] = 5;
}
Run Code Online (Sandbox Code Playgroud)

它的工作原理.

在初始化中

int B[10] = {NULL};
Run Code Online (Sandbox Code Playgroud)

int用a void*(NULL)初始化a .由于是从有效转换void*int,这样的作品,但它是不是很洁净,因为转换是实现定义的,并且通常由程序员显示一个错误,因此编译器警告一下.

int B[10] = {0};
Run Code Online (Sandbox Code Playgroud)

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


Alb*_*nto 11

也许你试图这样做?

#include <stdio.h>

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;
}
Run Code Online (Sandbox Code Playgroud)

  • 这是我一直在寻找的答案。为什么要使用 `array + 2` 而不是 `array[2]`? (2认同)

gre*_*ggo 5

如果您实际上想传递数组指针,则

#include <stdio.h>

void 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;
}
Run Code Online (Sandbox Code Playgroud)

但是,正如其他答案中提到的那样,执行此操作并不常见。通常,仅当您要传递2D数组时才传递指向数组的指针,如下所示,它在这里看起来更加清晰。实际上将2D数组作为指向其第一行的指针传递。

void 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] )
}
Run Code Online (Sandbox Code Playgroud)

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;
}
Run Code Online (Sandbox Code Playgroud)

通常这样做是为了避免眼睛疲劳:

typedef int (*pa10int)[10];

pa10int select_myarr_row( int i ) {
   return (i>=0 && i<5)? &MyArr[i] : &MyRow;
}
Run Code Online (Sandbox Code Playgroud)