I have the following C++ code:
#include <iostream>
using namespace std;
int main(){
}
int findH(int positionH[]){
return positionH; //error happens here.
}
The compiler throws an error:
invalid conversion from `int*' to `int'
What does this error mean?
asked Dec 4, 2012 at 15:18
5
positionH[]
is an array, and its return type is int
.
The compiler will not let you do that. Either make the parameter an int:
int findH(int positionH){
return positionH;
}
Or make the return type a pointer to an int:
int* findH(int positionH[]){
return positionH;
}
Or convert the array to an integer before return:
int findH(int positionH[]){
return positionH[0];
}
answered Dec 4, 2012 at 15:22
PavenhimselfPavenhimself
5171 gold badge5 silver badges18 bronze badges
This line is invalid C++ (and invalid C too, which your code appears to be written in):
int bla[2] = findH(field, positionH);
bla is an array of 2 elements and cannot be initialised that way. findH returns int.
answered Dec 4, 2012 at 15:45
CashCowCashCow
30.8k5 gold badges60 silver badges92 bronze badges
0
This error is coming while you are trying to do:
int *p =10;
that means you are assigning int
value to pointertoint *p
.
But pointer is storing address that means *p
is taking 10
as address.
So
just do:
int i=10;
int *p=&i;
or
p=&i;
it will not give any error.
Fábio
7712 gold badges14 silver badges25 bronze badges
answered May 21, 2019 at 7:51
0
The error was caused because you returned a pointer and the compiler is expecting a int.
There is a very BIG difference between int * and int.
Also why are you returning positionH, arrays are passed by reference, there is no need to return it.
Better code would be
void option1(char** field, int[])
{
int findH(char **, int[]);
int positionH[2];
findH(field, positionH);
//positionH passed by reference, no need to return it
}
void findH(char **field, int positionH[])
{
for(int n = 0;n < 14 ; n++)
{
for(int m = 0; m < 14; m++)
{
if(field[m][n] == 'H')
{
positionH[0] = n;
positionH[1] = m;
}
}
}
}
answered Dec 4, 2012 at 15:23
Desert IceDesert Ice
4,4215 gold badges31 silver badges58 bronze badges
3
In this documentation, we will discuss the common programming error «invalid conversion from int* to int». We will understand the root cause of this error, how to identify it, and provide step-by-step solutions to fix the issue.
Table of Contents
- What is the Error?
- Identifying the Error
- Solving the Error
- FAQs
- Related Links
What is the Error?
The error «invalid conversion from int* to int» occurs when you try to assign an int
pointer to an int
variable. In C++ and other programming languages, data types and their respective pointers are not interchangeable, and attempting to do so will lead to a compilation error.
Identifying the Error
This error is quite easy to identify as it is explicitly mentioned in the error message generated by the compiler. The error message typically looks like this:
error: invalid conversion from 'int*' to 'int' [-fpermissive]
The error message will also indicate the line number where the issue is found, which will help you quickly identify the problematic code segment.
Solving the Error
To solve this error, you need to make sure that you are not assigning an int
pointer to an int
variable. Follow the steps below to fix this issue:
- Locate the problematic code segment using the line number indicated in the error message.
- Identify the assignment statement that is causing the error. It will most likely look like this:
int x;
int* y;
x = y; // Error: Invalid conversion from 'int*' to 'int'
To fix the error, you have two options:
a. If you want to assign the value of the pointer (i.e., the memory address) to the int
variable, you can use explicit type casting:
x = (int)y; // Assign the memory address to x
b. If you want to assign the value stored at the memory address of the pointer to the int
variable, you should use the dereference operator *
:
x = *y; // Assign the value stored at the memory address of y to x
- Verify that the error is resolved by recompiling your code.
FAQs
1. What is a pointer?
A pointer is a variable that stores the memory address of another variable. Pointers are used to directly access and manipulate memory, which can significantly improve performance in some cases.
2. How do I declare an int pointer?
To declare an int
pointer, you need to use the int*
data type followed by the pointer variable’s name. For example:
int* p;
3. What is the purpose of the dereference operator (*) in C++?
The dereference operator *
is used to access the value stored at the memory address pointed to by a pointer. When you use the dereference operator with a pointer, it returns the value stored at the memory address.
4. What is type casting?
Type casting is a technique used to convert a variable from one data type to another. In C++, you can use either explicit type casting or implicit type casting (also known as type conversion).
5. What is the difference between an int and an int pointer?
An int
is a primitive data type that represents a whole number, while an int
pointer is a variable that holds the memory address of an int
variable. They are not interchangeable, and attempting to assign an int
pointer to an int
variable or vice versa will result in a compilation error.
- C++ Pointers
- Type Casting in C++
- Dereference Operator in C++
- Pointer Preliminaries in C++
- the Conversion Error
- Resolve the Conversion Error
This short tutorial will discuss the error message "Invalid conversation of int* to int"
. First, let’s have a recap of the pointers in C++.
Pointer Preliminaries in C++
Pointers are used to hold the address (a hexadecimal value) of a variable, and it is assigned to the pointer type variable using the ampersand sign(&
), also known as the address operator preceded to the variable name.
Pointers are declared using the *
symbol like this:
We can assign the address of an integer variable to an integer pointer using the following statement:
The above line of code will assign the address of the integer variable a
to the integer pointer p
.
the Conversion Error
When an integer variable is assigned a hexadecimal address value of variable instead of an integer type value, the “invalid conversion from int* to int”
error occurs.
Example Code:
#include <iostream>
using namespace std;
int main()
{
int a=10;
int p;
p=&a; // invalid conversion error
cout<< p;
}
The above code will generate a conversion error on line 07 as p
is assigned an address of type int*
that can’t be stored in an integer variable.
Output:
main.cpp: In function 'int main()': main.cpp:7:7: error: invalid conversion from 'int*' to 'int' [-fpermissive] ptr = &p; //invalid conversion. ^
Resolve the Conversion Error
Most compilers don’t allow a type casting from pointer type to the simple datatype. Therefore, the issue can be solved by ensuring that the address type value is assigned to a proper pointer variable.
Example Code:
#include <iostream>
using namespace std;
int main()
{
int a=10;
int* p;
p=&a;
cout<< p;
}
Run Code
The *
symbol while declaring p
will make it a pointer to an integer. Thereby making it capable of storing the address of an integer variable without requiring any type-conversions.
Trusted answers to developer questions
Educative Answers Team
Grokking the Behavioral Interview
Many candidates are rejected or down-leveled in technical interviews due to poor performance in behavioral or cultural fit interviews. Ace your interviews with this free course, where you will practice confidently tackling behavioral interview questions.
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.
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 ©2023 Educative, Inc. All rights reserved
Trusted Answers to Developer Questions
Related Tags
error
solution
invalid conversion
Learn in-demand tech skills in half the time
Copyright ©2023 Educative, Inc. All rights reserved.
Did you find this helpful?
- Forum
- General C++ Programming
- Invalid conversion from int to int*
Invalid conversion from int to int*
I’m using a MinGW compiler on Windows XP.
Would anyone know why this would cause an error.
The error is: ‘Invalid conversion from int to int*.
in my main function…
int *x = 5;
Yes, I know it’s simple, but that’s it.. Why would I get this error?
Thanks
you are declaring an
x
variable which has a time of «pointer to int» (
int*
)
Then you are trying to assing integer (
int
) value
5
Type mismatch.
I’m sorry but I don’t understand your reply.
I didn’t know that I could not create a pointer to an integer like this.
Could you reexplain the above, I’m just now sure what you’re telling me. Thanks
|
|
Last edited on
int* x
is a pointer.
Pointers store memory addresses.
What does it mean to store «5» as a memory address?
If you want an integer variable, use int x = 5;
.
If you want a pointer to a variable whose value is 5, use either
|
|
or int* x = new int(5);
(but if you do that last one, you have to delete x;
after you’re done).
If you want a pointer to the memory address 0x5 (not sure why — that’s not a spot you should be tampering with), try int* x = reinterpret_cast<int*>(5);
.
So if I understand correctly the compiler will see the 5 as a memory location which is why I need to initiate int pointers differently.
Is that correct?
Thanks for your response. That’s a good explanation.
Topic archived. No new replies allowed.