1

So basically I want to be able to do something along these lines

char *test1 = "hey";
int test2 = (int)test1;
char *test3 = (char*) &test2;
printf("%s", test3);
// have the output as hey

Is this even possible? I know this isn't working correctly but I just want to know if there's a working method. Yes I want to use char pointers and ints, so no, I don't want to use strings

5
  • 1
    C++: Is it safe to cast pointer to int and later back to pointer again? Commented Apr 13, 2016 at 13:07
  • 1
    Note that char *test1 = "hey"; should not compile. Some compilers have an extension that allows this but you should use char test1[] = "hey"; or const char* test1 = "hey"; or better yet std::string test = "hey"; Commented Apr 13, 2016 at 13:07
  • 1
    Is what even possible? You've written some code that does some iffy casts that may or may not do something sensible. What is it that you're trying to accomplish? Commented Apr 13, 2016 at 13:31
  • Out of interest, why do you want to do this? Commented Apr 13, 2016 at 13:32
  • @JohnnyMopp - while that's an important point, it's not really what's going on here. There's another level of indirection, initializing test3 with (char*)&test2. Note the & -- it's taking the address of test2. Commented Apr 13, 2016 at 13:32

2 Answers 2

2
char *test1 = "hey";
int test2 = (int)test1;
char *test3 = (char*) test2; // Note that the ampersand has been removed
printf("%s", test3);

might work if ints and pointers are the same size (they often are, but it's not guaranteed).

But when you assign test3, you are taking the address of test2, instead of its value, which is what I think you really meant to do.

Sign up to request clarification or add additional context in comments.

10 Comments

Pointers are usually unsigned integers.
Please check the answer as "accepted" if you want to give the deserved reputation to the user ;)
@AhmetIpkin this answer exhibits undefined behaviour and should not be accepted. casting a pointer to an int is undefined behaviour. casting a string literal to a pointer to mutable char is undefined behaviour. casting a string literal (indirectly) to a pointer to mutable char is also undefined behaviour.
@RichardHodges Casting a char * to an int is NOT an undefined behaviour (std::intptr_t is optional and most of the time is uint_t) , you are simply reading the address of the literal (modifying that address is UB). Castings are pretty well defined operations on C / C++. Casting a string literal to a char pointer is also NOT an UB, it is an ERROR in any sane compiler - which can be fixed with assigning to a const char*.
@AhmetIpkin this question is tagged c++.
|
1

The code represents undefined behaviour and is therefore incorrect.

There is, however a way to do what you want legally. See comments inline for explanations:

#include <cstddef>
#include <cstdint>
#include <cstdio>

int main()
{
    // string literals are const
    const char *test1 = "hey";

    // intptr_t is the only int guaranteed to be able to hold a pointer
    std::intptr_t test2 = std::intptr_t(test1);

    // it must be cast back to exactly what it was
    const char *test3 = reinterpret_cast<const char*>(test2);

    // only then will the programs behaviour be well defined
    printf("%s", test3);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.