# Thread: CAST question

1. ## CAST question

What's the difference betwem

Code:
```int address = 1000;
int *ptr = (int*)address;```
and

Code:
```int address = 1000;
int *ptr = address;```

- It may be because I haven't yet used CAST, but please explain.

Furthermore I don't get what the point is about this:
int *ptr = (int *)02400000;

(It's the exact same thing I asked in this thread http://cboard.cprogramming.com/showt...threadid=30419 but since I got no results I ask once more)

2. In your example address is not an int pointer it is an int. So you use a cast to tell the compiler that you want the int that address contains to be used as a pointer value. That is all a cast does.

3. The first one is assigning the pointer to an address. The second is assigning a value to the memory pointed to by the pointer.

int address = 1000;
int *ptr = (int*)address;

is equivelent to:

ptr = (int *)address;

It just doesn't look like that because you're declaring and defining on the same line.
(Don't point a pointer to an arbitrary place in memory like that btw, it will (should) crash your program).

int *ptr = (int *)02400000;

is equivelent to:

int *ptr;
ptr = (int *)02400000;

C++ won't let you have different datatypes on each side of an equals sign unless there is a one step conversion (Correct me if I'm wrong on that). The (int *) turns the integer 02400000 into an address of an integer.

4. Casting is the process of telling your compiler that you know better than it does. Thats all there is to it.
The casts shown here are old style c casts. Correctly written in c++ it would look like this...
Code:
```int address=1000;
here we make an int but we would like to use this int as an address so we tell the compiler to reinterpret that int as an int* instead. address still holds an int but ptr holds the int* representation of that int. This is a poor example of casting really.The times you will need to do this can almost certainly be counted on no fingers.
In your second example I expect you will get a compiler error saying that there is no valid conversion between int and int*.

All c/c++ texts will have examples of casting.Have a read through your books.Sometimes it is very necessary to cast especially in windows programming where you often recieve a pointer to a struct as a long. This cannot be used as a pointer until it has been cast back to a pointer to the correct type.

5. There is nothing wrong with C-style casts. In most cases they're sufficient and do the exact same thing as the C++ version, (and a hell of a lot quicker to type ). Just a matter of taste.

6. There is nothing wrong with C-style casts.
Except they are harder to spot than something that says _cast and also you cannot know whether they failed whereas you can with c++ style casts.

7. Originally posted by Stoned_Coder
you cannot know whether they failed
failed? C-style casts don't fail.

8. A C style cast can fail. they have rules to follow too. Try a few dynamic casts but c style. Try ones that will fail with bad_cast exception if dynamic_cast is used.

9. are you sure? show me a cast that fails.

10. You could have done this yourself. I gave instructions. Lazy!
Code:
```#include <iostream>
using namespace std;

class base
{
public:
virtual void func() const = 0;
};

class real_derive_a : public base
{
public:
real_derive_a(){}
virtual void func() const {cout << "real_derive_a" << endl;}
};

class real_derive_b : public base
{
public:
real_derive_b(){}
virtual void func() const {cout << "real_derive_b" << endl;}
};

class imag_derive
{
public:
imag_derive(){}
virtual void func() const {cout << "imag_derive" << endl;}
};

int main()
{
base* pBase = new real_derive_a;

real_derive_b* pRealB;

pRealB = (real_derive_b*)pBase;
if(pRealB == 0)
cout << "C style cast failure" << endl;
else
pRealB->func(); // function executes but cast fails. Which function would this have called if cast didnt fail?
pRealB = dynamic_cast<real_derive_b*>(pBase);
if(pRealB == 0)
cout << "dynamic cast failure" << endl;
else
pRealB->func();

imag_derive* pImag;

pImag = (imag_derive*)pBase;
if(pImag == 0)
cout << "C style cast failure" << endl;
else
pImag->func(); // same here.
pImag = dynamic_cast<imag_derive*>(pBase);
if(pImag == 0)
cout << "dynamic cast failure" << endl;
else
pImag->func();

delete pBase;
return 0;
}```

11. ehh? that cast didnt fail. it did exactly what it was supposed to do. the fact that that wasnt what you wanted it to do is irrelevant. like i said before, C casts do not fail. it is the nature of them: explicit conversion from one data type to another, whether or not that conversion makes any sense at all.

12. how if you explicitly cast a base* to a real_derive_b* does the function real_derive_a::func() get called when real_derive_b is not a base of real_derive_a ??
how if you explicitly cast a base* to a imag_derive* does the function real_derive_a::func() get called when imag_derive is not a base of any class??

Its because the casts fail.

13. ...In most cases they're sufficient and do the exact same thing as the C++ version...
Well, it's true there are some reasons that you wouldn't want to use a C style cast, Stoner's example being one of them. However, most of the time it really doesn't matter much which you choose. And I really disagree with the argument that "it's more readable" to use the C++ version. Sloppy code can't be saved by a static_cast<>, and well-written code won't be obscured by a (Foo*) one.

14. >Its because the casts fail.

I guess that's just a point of view. The cast itself will not fail. If you cast an int 1000 to a pointer to hashtable, the 1000 will be correctly converted to a pointer to hashtable. Calling any function on the pointer will fail, because the cast was nonsense from the start. However, the cast itself did not fail, what fails is the operation right after the cast.

As said above, the C++ Version might be safer, but a good programmer can use both safely and a bad programmer will crash his programs using either style. I have seen so many programs that used C++ style casts, but did not catch exceptions. What is it good for then ? It will crash your app anyway, even if it notices the error and throws a bad_cast exception that isn't caught.

15. For the most part this is an issue of preference. I actually try and use C++ style coding for C++ and C style coding for C but some do not. The bottom line is that it doesn't matter most of the time. Some of the time it is better. One advantage of using C++ style typecasting is that you are very specific as to what you are doing. It makes your intentions obvious to anyone else who reads your code.

Popular pages Recent additions