casting - error_ cast from 'char_' to 'int' loses precision [-fpermissive] on using reinterpret_cast (C++) - Stack Overflow
casting - error_ cast from 'char_' to 'int' loses precision [-fpermissive] on using reinterpret_cast (C++) - Stack Overflow
Just browsing Stack Overflow? Help us improve your experience. Sign up for research
The community reviewed whether to reopen this question 3 years ago and left it closed:
Duplicate This question has been answered, is not unique, and doesn’t differentiate itself
from another question.
#include <iostream>
#include <typeinfo>
using namespace std;
int main()
{
int i;
char *p = "This is a string";
i = reinterpret_cast<int>(p);
cout << i;
return 0;
}
Share Improve this question Follow asked Sep 14, 2021 at 10:46
Arpitha Prakash
45 1 5
1 of 4 4/12/2024, 7:34 pm
casting - error: cast from 'char*' to 'int' loses precision [-fpermissive] ... https://stackoverflow.com/questions/69176330/error-cast-from-char-to...
1 casting should be avoided when possible. – Jarod42 Sep 14, 2021 at 10:48
2 char *p = "This is a string"; -> const char *p = "This is a string"; – Jarod42 Sep 14,
2021 at 10:48
1 std::/*u*/intptr_t seems more appropriate than int . – Jarod42 Sep 14, 2021 at 10:49
2 This question is not a duplicate of "How can I convert a std::string to int?". There's no mention of
std::string here, but most importantly, the OP isn't trying to convert a string to an int; rather, they
are trying to understand how reinterpret_cast works. – Fabio says Reinstate Monica Sep 14, 2021 at
11:48
2 of 4 4/12/2024, 7:34 pm
casting - error: cast from 'char*' to 'int' loses precision [-fpermissive] ... https://stackoverflow.com/questions/69176330/error-cast-from-char-to...
3
Most importantly, the message means that the program is ill-formed. A conversion from
the type char * into the type int is not defined in the language (in this case).
The message also contains extra detail "loses precision". From that we can deduce that
there can be more memory addresses representable by the pointer type than there are
numbers representable by the integer type (in this particular implementation of the C++
language). This detail is the reason why the conversion isn't allowed. Reinterpret cast from
pointer to integer is defined only in the case where the integer can represent all of the
possible values of the pointer type. In language implementations where such integer type
exists, there is a type alias for the type in the standard library: std::uintptr_t
This implicit conversion is also ill-formed. A string literal, which is an array of const char is
not implicitly convertible to char* in C++ (since C++11; prior to that such conversion was
allowed but deprecated).
Can you please provide a snippet of code where reinterpret_cast is used correctly?
Correct uses of reinterpret_cast are extremely rare. There are many rules restrictions on its
use, and in order to be able to write a correct program, you must understand all of the
relevant rules and their subtleties.
I can show an example, but you must understand that making any change, no matter how
subtle, has very high chance of producing an incorrect program. Here is the example, as
promised:
struct standard_layout_struct {
int first_member;
};
int main() {
standard_layout_struct instance { .first_member=42 };
standard_layout_struct* pointer_to_instance = &instance;
int* pointer_to_member = reinterpret_cast<int*>(pointer_to_instance);
std::cout << *pointer_to_member;
}
3 of 4 4/12/2024, 7:34 pm
casting - error: cast from 'char*' to 'int' loses precision [-fpermissive] ... https://stackoverflow.com/questions/69176330/error-cast-from-char-to...
But isn't the purpose of reinterpret_cast to convert from one type to another radically different
Share Improve this answer edited Sep 14, 2021 at 11:11 answered Sep 14, 2021 at 10:50
type? Can you please provide a snippet of code where reinterpret_cast is used correctly?
Follow eerorika
– Arpitha Prakash Sep 14, 2021 at 11:01
238k 12 206 345
1 No reinterpret_cast is telling you know better then the compiler. And should only be used sparingly
and with caution, sometimes its the only way to connect to legacy code. Or for example to overlay
structure information on top of a byte array (e.g a stream of data from the internet) – Pepijn Kramer
Sep 14, 2021 at 11:02
1 @ArpithaPrakash reinterpret_cast is legal only in specific cases. They are, for instance,
enumerated here: en.cppreference.com/w/cpp/language/reinterpret_cast. – Daniel Langr Sep 14,
2021 at 11:04
1 In other words reinterpret cast it is not something that does magical conversions for you. Quite the
opposite, you have to know what a piece of memory looks like and then you tell the compiler, treat
that bit of memory as if it was this type. – Pepijn Kramer Sep 14, 2021 at 11:05
To convert strings to values, a typecast wont work. The input string needs to be parsed, the
standard library can do this for you like this:
2
#include <iostream>
#include <string>
int main()
{
std::string str{ "42" };
auto value = std::stoi(str);
std::cout << value;
return 0;
}
Share Improve this answer Follow answered Sep 14, 2021 at 11:01
Pepijn Kramer
12.5k 3 11 23
4 of 4 4/12/2024, 7:34 pm