Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ passing an array pointer as a function argument

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*)'
like image 987
Ortharios Avatar asked Aug 06 '12 14:08

Ortharios


People also ask

How do you pass an array pointer to a function?

To pass an array as a parameter to a function, pass it as a pointer (since it is a pointer). For example, the following procedure sets the first n cells of array A to 0. Now to use that procedure: int B[100]; zero(B, 100);

Can one-dimensional array be passed as function arguments in C language?

Both one-dimensional arrays and multidimensional arrays can be passed as function arguments.

Can you pass an array as an argument?

Arrays can be passed as arguments to method parameters. Because arrays are reference types, the method can change the value of the elements.

Can we pass pointer as an argument to function?

Just like any other argument, pointers can also be passed to a function as an argument.


3 Answers

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.

like image 185
Paul R Avatar answered Oct 10 '22 02:10

Paul R


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 (*aptr)[N]

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

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);
}
like image 40
Benjamin Lindley Avatar answered Oct 10 '22 03:10

Benjamin Lindley


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.

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.

like image 33
Sergey Kalinichenko Avatar answered Oct 10 '22 01:10

Sergey Kalinichenko