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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11829830/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 15:35:55  来源:igfitidea点击:

C++ passing an array pointer as a function argument

c++arraysfunctionpointers

提问by Ortharios

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*)'

回答by Paul R

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.

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

回答by Benjamin Lindley

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

回答by dasblinkenlight

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 pointerswhen 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.

您不需要将指向数组的指针传递给数组生成函数,因为当您将数组传递给函数时,数组已经衰减为指针。只需创建 parameter int 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.

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

回答by Crispin

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]。这两种方式都会奏效。我想这和康拉德·鲁道夫说的一样,想得差不多。

回答by wasim

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