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

时间:2021-03-30 21:44:40

I want to pass the B int array pointer into func function and be able to change it from there and then view the changes in main function

我想将B int数组指针传递到func函数中,并能从那里改变它,然后查看主函数的变化。

#include <stdio.h>

int func(int *B[10]){

}

int main(void){

    int *B[10];

    func(&B);

    return 0;
}

the above code gives me some errors:

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

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]'|

EDIT: new code:

编辑:新代码:

#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;
}

now i get these errors:

现在我得到了这些错误:

||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 ===|

6 个解决方案

#1


30  

In your new code,

在你的新代码中,

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

B is a pointer to int, thus B[0] is an int, and you can't dereference an int. Just remove the *,

B是一个指向int的指针,因此B[0]是一个整数,你不能取消一个int数。

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

and it works.

和它的工作原理。

In the initialisation

在初始化

int B[10] = {NULL};

you are initialising anint with a void* (NULL). Since there is a valid conversion from void* to int, that works, but it is not quite kosher, because the conversion is implementation defined, and usually indicates a mistake by the programmer, hence the compiler warns about it.

您正在使用void* (NULL)初始化anint。由于从void*到int的有效转换是有效的,但它不是很好,因为转换是定义的实现,通常表示程序员会犯错误,因此编译器会警告它。

int B[10] = {0};

is the proper way to 0-initialise an int[10].

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

#2


9  

Maybe you were trying to do this?

也许你想这么做?

#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;
}

#3


3  

If you actually want to pass an array pointer, it's

如果你想传递一个数组指针,它是。

#include <stdio.h>

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;
}

But as mentioned in other answers, it's not that common to do this. Usually a pointer-to-array is passed only when you want to pass a 2d array, where it suddenly looks a lot clearer, as below. A 2D array is actually passed as a pointer to its first row.

但正如其他答案所提到的,这样做并不常见。通常只有当您想要传递一个2d数组时,才会传递一个pointerto -数组,在这个数组中,它突然看起来更清晰,如下所示。一个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] )
}

The parameter of func may be declared as int B[5][10], int B[][10], int (*B)[10], all are equivalent as parameter types.

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

Addendum: you can return a pointer-to-array from a function, but the syntax to declare the function is very awkward, the [10] part of the type has to go after the parameter list:

Addendum:您可以从函数返回一个pointerto -array,但是要声明函数的语法非常笨拙,该类型的[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;
}

This is usually done as below, to avoid eyestrain:

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

typedef int (*pa10int)[10];

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

#4


1  

In new code assignment should be,

在新的代码分配中,

B[0] = 5

In func(B), you are just passing address of the pointer which is pointing to array B. You can do change in func() as B[i] or *(B + i). Where i is the index of the array.

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

In the first code the declaration says,

在第一个代码中,声明说,

int *B[10]

says that B is an array of 10 elements, each element of which is a pointer to a int. That is, B[i] is a int pointer and *B[i] is the integer it points to the first integer of the i-th saved text line.

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

#5


0  

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;
}

#6


-1  

In the function declaration you have to type as

在函数声明中,必须键入as。

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

#1


30  

In your new code,

在你的新代码中,

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

B is a pointer to int, thus B[0] is an int, and you can't dereference an int. Just remove the *,

B是一个指向int的指针,因此B[0]是一个整数,你不能取消一个int数。

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

and it works.

和它的工作原理。

In the initialisation

在初始化

int B[10] = {NULL};

you are initialising anint with a void* (NULL). Since there is a valid conversion from void* to int, that works, but it is not quite kosher, because the conversion is implementation defined, and usually indicates a mistake by the programmer, hence the compiler warns about it.

您正在使用void* (NULL)初始化anint。由于从void*到int的有效转换是有效的,但它不是很好,因为转换是定义的实现,通常表示程序员会犯错误,因此编译器会警告它。

int B[10] = {0};

is the proper way to 0-initialise an int[10].

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

#2


9  

Maybe you were trying to do this?

也许你想这么做?

#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;
}

#3


3  

If you actually want to pass an array pointer, it's

如果你想传递一个数组指针,它是。

#include <stdio.h>

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;
}

But as mentioned in other answers, it's not that common to do this. Usually a pointer-to-array is passed only when you want to pass a 2d array, where it suddenly looks a lot clearer, as below. A 2D array is actually passed as a pointer to its first row.

但正如其他答案所提到的,这样做并不常见。通常只有当您想要传递一个2d数组时,才会传递一个pointerto -数组,在这个数组中,它突然看起来更清晰,如下所示。一个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] )
}

The parameter of func may be declared as int B[5][10], int B[][10], int (*B)[10], all are equivalent as parameter types.

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

Addendum: you can return a pointer-to-array from a function, but the syntax to declare the function is very awkward, the [10] part of the type has to go after the parameter list:

Addendum:您可以从函数返回一个pointerto -array,但是要声明函数的语法非常笨拙,该类型的[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;
}

This is usually done as below, to avoid eyestrain:

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

typedef int (*pa10int)[10];

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

#4


1  

In new code assignment should be,

在新的代码分配中,

B[0] = 5

In func(B), you are just passing address of the pointer which is pointing to array B. You can do change in func() as B[i] or *(B + i). Where i is the index of the array.

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

In the first code the declaration says,

在第一个代码中,声明说,

int *B[10]

says that B is an array of 10 elements, each element of which is a pointer to a int. That is, B[i] is a int pointer and *B[i] is the integer it points to the first integer of the i-th saved text line.

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

#5


0  

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;
}

#6


-1  

In the function declaration you have to type as

在函数声明中,必须键入as。

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