How to resolve the error: ​"invalid conversion from int to int*"

The “invalid conversion from into to int*” ​error is encountered when an integer pointer is ​assigned an integer value.

Remember: pointers are used to store the address of a particular variable.

svg viewer

Code

Error

In the code below, when we try to assign an integer value (i.e., p ) to an integer pointer,​ it throws an error.

#include <iostream>
using namespace std;
int main() {
int p = 20;
int* ptr;
ptr = p; //Invalid conversion.
}

​### Correct way

To resolve this error, assign the address of the variable p by using the & symbol.

 int p = 20;
 int* ptr;
 ptr = &p; 

The & symbol is used to return the address of a particular variable.

Copyright ©2024 Educative, Inc. All rights reserved