Error: "cannot Convert 'int*' to 'int**"

Asked

Viewed 1,368 times

0

I am trying to send the address of an array to a created function (which has as parameter a pointer that is also an array). The error is as follows:

main.cpp: In function 'int main()':
main.cpp:30:28: error: cannot convert 'int*' to 'int**' for argument '1' to 'int somadiferenca(int**)'
   somadiferenca(&numaboa[2]);    
exit status 1

I am compiling with repl.it, but the error is basically the same when compiling elsewhere.

The code is as follows::

#include <iostream>
using namespace std;

int somadiferenca(int *vetor[2])
{        
    int x;
    int y;

    x = (*vetor[0]) + (*vetor[1]);
    y = (*vetor[0]) - (*vetor[1]);

    if(y<0)
    {                
        y = -y;
        cout << x << endl << y;
    }
    else
    {
        cout << x << endl << y;
    }    
}
int main()
{
   int numaboa[2];

   cin >> numaboa[0];
   cin >> numaboa[1];

   somadiferenca(&numaboa[2]);

  return 0;

}
  • I found your code organization very confusing. I believe the argument of somadiferenca should only be a two-position vector, passing as parameter the variable numaboa

  • 1

    The idea was to test the use of pointers along with arrays, but it didn’t work. I want to understand why it didn’t work with the pointer, but I could do without it easily. Thanks for the answer, Jefferson ^^

2 answers

2


Miguel, it turns out that &numaboa[2] is the address of the index cell 2. You mean, &numaboa[0] is the same as numaboa (address of the first cell), &numaboa[1] is the same as numaboa+1 (address of the second cell) and &numaboa[2] is the same as numaboa+2 (I mean, it points to a cell that doesn’t even exist).

In addition, the parameter int v[] indicates that v is a pointer because the array symbol (or vector) is treated as a constant pointer that is worth the address of the first cell, that is, the parameter int *vetor[2] indicates that vetor is a pointer type to another pointer, this for whole.

So, know that if the intention is to use the values of the cells then you can pass numaboa as argument in a type parameter int* (that is, function can be void somadiferenca( int *vetor ) or void somadiferenca( int vetor[] ) or void somadiferenca( int vetor[2] )) and then access the two cells using vetor[0] and vetor[1].

A valid way to implement everything is as follows, that somadiferenca accepts pointer, the somadiferenca(numaboa) in fact does pointer pass and within the function does cell access via pointer type parameter.

#include <iostream>
using namespace std;

int somadiferenca( int *vetor ){        
    int x = vetor[0] + vetor[1] ;
    int y = vetor[0] - vetor[1] ;
    if( y<0 ){       
        cout << x << endl << (-y) ;
    }
    else {
        cout << x << endl << y;
    }    
}

int main(){
   int numaboa[2] ;
   cin >> numaboa[0] ;
   cin >> numaboa[1] ;
   somadiferenca( numaboa ) ;
   return 0 ;
}

Remembering some basic things:

1) int numaboa[2] is the definition of two temporary local cells in the function that are allocated consecutively in memory (execution stack);

2) numaboa is the constant (unalterable) pointer that always points to these two cells (numaboa is worth the address of the first, via index is addressed to others) seeing them as array;

3) numaboa[i] is an array cell and numaboa+i is the same as &(numaboa[i]), i.e., the cell address numaboa[i];

4) Parameters int vector[] and int *vector are the same thing, both pointers, after all arrays symbols like vetor allocated on the stack are treated as pointers.

Okay? Any doubt?

  • VERY GOOD!!!!!!! NO DOUBT!!!!!

0

How about:

#include <iostream>

using namespace std;

void somadiferenca( int vetor[2] )
{
    int x = vetor[0] + vetor[1];
    int y = vetor[0] - vetor[1];

    if( y < 0 )
        y = -y;

    cout << "Soma: " << x << endl;
    cout << "Diferenca: " << y << endl;
}

int main()
{
    int numaboa[2];

    cout << "a: ";
    cin >> numaboa[0];

    cout << "b: ";
    cin >> numaboa[1];

    somadiferenca( numaboa );

    return 0;
}

Or:

#include <iostream>

using namespace std;

void somadiferenca( int * vetor )
{
    int x = *vetor + *(vetor+1);
    int y = *vetor - *(vetor+1);

    if( y < 0 )
        y = -y;

    cout << "Soma: " << x << endl;
    cout << "Diferenca: " << y << endl;
}

int main()
{
    int numaboa[2];

    cout << "a: ";
    cin >> numaboa[0];

    cout << "b: ";
    cin >> numaboa[1];

    somadiferenca( &numaboa[0] );

    return 0;
}
  • Man, thanks for answering!!! : ) That way I know it works. It is that, theoretically, in this case, the use of pointers would kind of optimize the size and execution time of the program. Type, in the sum difference function you created variables, which in a way you won’t even use (they only serve to define how the function will behave). Then in main you created other variables, which theoretically will be copied into the somadiferenca void (it already takes time and space) for her to do what she has to do. Only then several variables were created unnecessarily. Correct me , I’m new to this of C++

  • With pointers (of course, the pointer occupies the space of the type of variable in which it is defined), you only give the address of the variables you created in main and it uses them itself. So to not create void variables that wouldn’t even be used, I created pointers, so that they could just redirect to the main variables and do what has to be done. I’m new to this C++, correct me if my reasoning is wrong. :)

  • Now that I saw that you did one with pointer!!!!! Thanks, I think I understood why my code did not work!!!!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.