c++作为函数参数传递数组指针

时间:2021-07-07 21:42:02

I'm trying to use pointers of arrays to use as arguments for a function which generates an array.

我正在尝试使用数组指针作为生成数组的函数的参数。

void generateArray(int *a[],  int *si){
  srand(time(0));
  for (int j=0;j<*si;j++)
       *a[j]=(0+rand()%9);
} //end generateArray;

int main() {
  const int size=5;
  int a[size];

  generateArray(&a, &size);

  return 0;
} //end main

But when I compile this this message appears:

但是当我编译它时,会出现以下消息:

cannot convert `int (*)[5]' to `int**' for argument `1' to `void generateArray(int**, int*)'

5 个解决方案

#1


55  

You're over-complicating it - it just needs to be:

你过度复杂了 - 它只需要:

void generateArray(int *a, int si)
{
    for (int j = 0; j < si; j++)
        a[j] = rand() % 9;
}

int main()
{
    const int size=5;
    int a[size];

    generateArray(a, size);

    return 0;
}

When you pass an array as a parameter to a function it decays to a pointer to the first element of the array. So there is normally never a need to pass a pointer to an array.

将数组作为参数传递给函数时,它会衰减到指向数组第一个元素的指针。因此通常不需要将指针传递给数组。

#2


17  

int *a[], when used as a function parameter (but not in normal declarations), is a pointer to a pointer, not a pointer to an array (in normal declarations, it is an array of pointers). A pointer to an array looks like this:

int * a [],当用作函数参数(但不在正常声明中)时,是指向指针的指针,而不是指向数组的指针(在正常声明中,它是指针数组)。指向数组的指针如下所示:

int (*aptr)[N]

Where N is a particular positive integer (not a variable).

其中N是特定的正整数(不是变量)。

If you make your function a template, you can do it and you don't even need to pass the size of the array (because it is automatically deduced):

如果你把你的函数作为模板,你可以做到,你甚至不需要传递数组的大小(因为它是自动推导出来的):

template<size_t SZ>
void generateArray(int (*aptr)[SZ])
{
    for (size_t i=0; i<SZ; ++i)
        (*aptr)[i] = rand() % 9;
}

int main()
{    
    int a[5];    
    generateArray(&a);
}

You could also take a reference:

你也可以参考一下:

template<size_t SZ>
void generateArray(int (&arr)[SZ])
{
    for (size_t i=0; i<SZ; ++i)
        arr[i] = rand() % 9;
}

int main()
{    
    int a[5];    
    generateArray(a);
}

#3


5  

You do not need to take a pointer to the array in order to pass it to an array-generating function, because arrays already decay to pointers when you pass them to functions. Simply make the parameter int a[], and use it as a regular array inside the function, the changes will be made to the array that you have passed in.

您不需要使用指向数组的指针以将其传递给数组生成函数,因为当您将数组传递给函数时,数组已经衰减为指针。只需将参数设置为a [],并将其用作函数内的常规数组,将对您传入的数组进行更改。

void generateArray(int a[],  int si) {
    srand(time(0));
    for (int j=0;j<*si;j++)
        a[j]=(0+rand()%9);
}

int main(){
    const int size=5;
    int a[size];
    generateArray(a, size);
    return 0;
}

As a side note, you do not need to pass the size by pointer, because you are not changing it inside the function. Moreover, it is not a good idea to pass a pointer to constant to a parameter that expects a pointer to non-constant.

作为旁注,您不需要通过指针传递大小,因为您没有在函数内部更改它。此外,将指向常量的指针传递给期望指针为非常量的参数并不是一个好主意。

#4


0  

I'm guessing this will help.

我猜这会有所帮助。

When passed as functions arguments, arrays act the same way as pointers. So you don't need to reference them. Simply type: int x[] or int x[a] . Both ways will work. I guess its the same thing Konrad Rudolf was saying, figured as much.

当作为函数参数传递时,数组的行为与指针相同。所以你不需要引用它们。只需输入:int x []或int x [a]。两种方式都有效。我猜这与康拉德鲁道夫所说的一样,算得太多了。

#5


-1  

This is another method . Passing array as a pointer to the function
void generateArray(int *array,  int size) {
    srand(time(0));
    for (int j=0;j<size;j++)
        array[j]=(0+rand()%9);
}

int main(){
    const int size=5;
    int a[size];
    generateArray(a, size);
    return 0;
}

#1


55  

You're over-complicating it - it just needs to be:

你过度复杂了 - 它只需要:

void generateArray(int *a, int si)
{
    for (int j = 0; j < si; j++)
        a[j] = rand() % 9;
}

int main()
{
    const int size=5;
    int a[size];

    generateArray(a, size);

    return 0;
}

When you pass an array as a parameter to a function it decays to a pointer to the first element of the array. So there is normally never a need to pass a pointer to an array.

将数组作为参数传递给函数时,它会衰减到指向数组第一个元素的指针。因此通常不需要将指针传递给数组。

#2


17  

int *a[], when used as a function parameter (but not in normal declarations), is a pointer to a pointer, not a pointer to an array (in normal declarations, it is an array of pointers). A pointer to an array looks like this:

int * a [],当用作函数参数(但不在正常声明中)时,是指向指针的指针,而不是指向数组的指针(在正常声明中,它是指针数组)。指向数组的指针如下所示:

int (*aptr)[N]

Where N is a particular positive integer (not a variable).

其中N是特定的正整数(不是变量)。

If you make your function a template, you can do it and you don't even need to pass the size of the array (because it is automatically deduced):

如果你把你的函数作为模板,你可以做到,你甚至不需要传递数组的大小(因为它是自动推导出来的):

template<size_t SZ>
void generateArray(int (*aptr)[SZ])
{
    for (size_t i=0; i<SZ; ++i)
        (*aptr)[i] = rand() % 9;
}

int main()
{    
    int a[5];    
    generateArray(&a);
}

You could also take a reference:

你也可以参考一下:

template<size_t SZ>
void generateArray(int (&arr)[SZ])
{
    for (size_t i=0; i<SZ; ++i)
        arr[i] = rand() % 9;
}

int main()
{    
    int a[5];    
    generateArray(a);
}

#3


5  

You do not need to take a pointer to the array in order to pass it to an array-generating function, because arrays already decay to pointers when you pass them to functions. Simply make the parameter int a[], and use it as a regular array inside the function, the changes will be made to the array that you have passed in.

您不需要使用指向数组的指针以将其传递给数组生成函数,因为当您将数组传递给函数时,数组已经衰减为指针。只需将参数设置为a [],并将其用作函数内的常规数组,将对您传入的数组进行更改。

void generateArray(int a[],  int si) {
    srand(time(0));
    for (int j=0;j<*si;j++)
        a[j]=(0+rand()%9);
}

int main(){
    const int size=5;
    int a[size];
    generateArray(a, size);
    return 0;
}

As a side note, you do not need to pass the size by pointer, because you are not changing it inside the function. Moreover, it is not a good idea to pass a pointer to constant to a parameter that expects a pointer to non-constant.

作为旁注,您不需要通过指针传递大小,因为您没有在函数内部更改它。此外,将指向常量的指针传递给期望指针为非常量的参数并不是一个好主意。

#4


0  

I'm guessing this will help.

我猜这会有所帮助。

When passed as functions arguments, arrays act the same way as pointers. So you don't need to reference them. Simply type: int x[] or int x[a] . Both ways will work. I guess its the same thing Konrad Rudolf was saying, figured as much.

当作为函数参数传递时,数组的行为与指针相同。所以你不需要引用它们。只需输入:int x []或int x [a]。两种方式都有效。我猜这与康拉德鲁道夫所说的一样,算得太多了。

#5


-1  

This is another method . Passing array as a pointer to the function
void generateArray(int *array,  int size) {
    srand(time(0));
    for (int j=0;j<size;j++)
        array[j]=(0+rand()%9);
}

int main(){
    const int size=5;
    int a[size];
    generateArray(a, size);
    return 0;
}