Error converting 'int' to 'int *'

1

I wanted to know why you are causing the error:

  

Error converting 'int' to 'int *'

This program is only for training the use of pointers in parameters.

void teste(int *t){

*t = 50;
}   

int main(){

int x = 10;

cout << "Sem usar o metodo com ponteiro: " << x << endl;

teste(x);

cout << "Usando o metodo com ponteiro: " << x << endl;  

return 0;   
}
    
asked by anonymous 30.04.2018 / 02:24

1 answer

2

You just have to say that you want to pass the address and not the value, after all the function is expecting an address of a value and not the value itself. Use & .

void teste(int *t) {
    *t = 50;
}   

int main() {
    int x = 10;
    cout << "Sem usar o metodo com ponteiro: " << x << endl;
    teste(&x);
    cout << "Usando o metodo com ponteiro: " << x << endl;   
}
    
30.04.2018 / 02:32