Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

error
solution
invalid conversion

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

Educative Answers Team

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.

RELATED TAGS

error
solution
invalid conversion
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring