0

I'm learning about type casting and finding it pretty tough to understand it. So from what I've learned. You cannot static cast pointers of one type to another. I don't know exactly why. Because you can static cast an int to a char which has there own address in memory so its super confusing me as to why you cannot do the same with pointers.

So with reinterpret cast. You can convert a pointer type to another pointer type. I think?

So a few questions i have are. You can convert non pointer variables with a static cast.

And You can cast pointers types to different pointer types using reinterpret?

Below are some examples of stuff I'm struggling on.

<int *>something // what is the * inside of the <int> meaning? 

// i know what the previous *<int> means to deference.

// So if i have this does this mean I'm casting something into a char pointer
// and storing the cast inside of ptr?

char* ptr = static_cast<char *>something 

//With reinterpret cast i can cast from another type like this 

int x = 9;
int* ptr = {&x};

char* nowchar = reinterpret_cast<char *>&x

// the above is fine? 

//But i cannot do the same with static

int x = 8;
int* ptr = {&x};

char* nowchar = static_cast<char*>&x;


Can you explain why i cannot use static on a pointer, and why reinterpret_cast works

  • 1
    Well, it's same reason why automobiles can't fly and airplanes can't drive on highways. Each one is designed for different things. – Sam Varshavchik Sep 05 '22 at 22:06
  • could you explain some more as to why thanks – peter evans Sep 05 '22 at 22:08
  • 1
    Broadly `static_cast<>` is for normal casting. What's normal? So casting a `double` to an `int` is a normal thing to be doing. That's not to say it is 100% safe because the value may be out of range but moving between arithmetic types is 'normal' within a type system. Now casting a pointer to a char* is always valid because on of the roles of `char` is to be byte and in C++ it's always valid to go to 'machine level' and treat memory as a sequence of bytes. But(!) it is an exercise in going round the type system so isn't in the set of 'type system condoned' static-casts! – Persixty Sep 05 '22 at 22:28
  • Okay i get you thank you – peter evans Sep 05 '22 at 22:37

1 Answers1

1

Taking this one question at a time:

<int *>something // what is the * inside of the <int> meaning?

When you are casting, what you put in the angle brackets is the type, int* (or int *) is a pointer to an integer. This is often confusing for those new to C++ because you also use the * operator to dereference a pointer, but in this context it denotes that the type is a pointer to an int and not an int.

// So if i have this does this mean I'm casting something into a char pointer
// and storing the cast inside of ptr?

char* ptr = static_cast<char *>something

Yes, the only case I'm aware of where this will work is if you are casting from / to a void* or if you are casting from a base class to a derived class, e.g.:

class Something{};
class SomethingElse : public Something{};

...

Something* something = new Something();
SomethingElse* ptr = static_cast<SomethingElse *>(something);

In this case, you are correct that you are casting the pointer to the instance of Something into a pointer for the derived class SomethingElse.

//With reinterpret cast i can cast from another type like this 

int x = 9;
int* ptr = {&x};

char* nowchar = reinterpret_cast<char *>(&x);

// the above is fine? 

//But i cannot do the same with static

int x = 8;
int* ptr = {&x};

char* nowchar = static_cast<char*>(&x);

In C++, static_cast is only used for a few (mostly) well-defined conversions. There is no well-defined conversion from a pointer to an int to a pointer to a char. Considering that neither the size (number of bits) of char nor int are specified, this makes sense. What does the compiler do if you are trying to cast a 16-bit int to an 8-bit char? In this case, what you need to do is reinterpret the int pointer to a char pointer; hence why you need to use a reinterpret_cast instead. Specifically, this will tell the compiler that it is allowed to reinterpret the underlying bit pattern of the data however it sees fit.

Layne Bernardo
  • 676
  • 3
  • 12