Pass by reference a vector [closed]

1

The problem is that I pass a vector by reference and the terminal tells me it's wrong.

Here are the respective codes and errors:

ordenavector(&vector, cont);
  

test13.c: 21: 6: warning: passing argument 1 of 'ordenavector' from incompatible pointer type [enabled by default]

void ordenavector(int *vector[20], int numero);
  

test13.c: 2: 6: note: expected 'int **' but argument is of type 'int (*) [20]'

    
asked by Gulycat 10.01.2017 в 19:40
source

2 answers

0

First of all it is not necessary to use a pointer to pass as a reference with a matrix, since the matrices and the pointers are compatible with each other with their castings ...

Example:

#include <stdio.h>

void SetMt(int* a, int b[])
{
  a[0] = 2;
  b[1] = 4;
}

int main()
{

    // creamos una matriz:
    int z[2]; // sz 2


    SetMt(z, z);
    printf("%d %d ", z[0], z[1]); // Salida: 24

}

I recommend that you read more about the pointers and matrices ...

    
answered by 10.01.2017 в 21:33
0

2 topics:

Do not specify the size of the vector in the parameter, the definition of the function should be:

void ordenavector(int *vector, int numero);

By defining the vector as int unvector[20]; , the name itself is already a reference to its position in memory, so the call:

ordenavector(unvector,20);

is more than enough. It is equivalent to:

ordenavector(&unvector[0],20);

where you pass the memory position of the first vector object, which matches the address to which unvector refers.

Notice that the following is also correct:

ordenavector(&unvector[10],10);

but not:

ordenavector(&unvector.20);

since you are passing a pointer to an integers pointer.

    
answered by 10.01.2017 в 21:51