1828343 Members
3089 Online
109976 Solutions
New Discussion

Typecasting in C++

 
Rajesh Parappil
Occasional Contributor

Typecasting in C++

Hi friends,

I have a problem in type conversion of a structure to character pointer type.

struct{
int a;
}test var;


cout<<(void*)var; /*Gives me the value*/

But if i do like this,

cout<<(char *)var;

I get nothing. What might be the problem?
Please give me a solution for this at the earliest.

Thanks & Best regards,
Rajesh P
7 REPLIES 7
A. Clay Stephenson
Acclaimed Contributor

Re: Typecasting in C++

First of all, your example is bad syntax so it won't actually compile.

I suppose you actually mean something like this:

cout << (void *) &var << endl;
cout << (char *) &var << endl;

In the first case, the output is actually the address of var. In the second case, you actually are getting output you simply don't recognize it as such. It's a null string. In the second case, the address is referencing a null string because that space happens to contain zeroes but because the space was not initialized it could contain pure garbage and you could also see a segmentation violation depending upon the implementation.
If it ain't broke, I can fix that.
Rajesh Parappil
Occasional Contributor

Re: Typecasting in C++

First of all thanks for giving me a reply.

Yes, that's what i meant.I don't get any segmentation error. I have a structure, but why it's required to allocate memory for that. Once i declare a object, the memory will be asssigned for that, right? Should i specifically allocate memory??





A. Clay Stephenson
Acclaimed Contributor

Re: Typecasting in C++

You are confusing a typedef (which doesn't allocate memory) with a variable declaration which does allocate memory. Because your variable is global (or file) scope, the memory is generally initialized as NUL's which is why the string didn't output as garbage. If you declared a variable with storage class auto (i.e. inside a function) then the space would have been taken off the stack and the contents would be undefined and most likely garbage.
If it ain't broke, I can fix that.
Rajesh Parappil
Occasional Contributor

Re: Typecasting in C++

Thanks for your reply.

Here my structure variable is declared in a function and before printing the pointer, I have assigned a value to the variable.

var.a = 10;

I think that eliminates the possibility of the variable "var" being a garbage. Please correct me if I am wrong.

Do you think it has to do anything with type casting?

Interestingly this is a copy paste code and was working in some other application.
A. Clay Stephenson
Acclaimed Contributor

Re: Typecasting in C++

You still aren't getting it. When you assign decimal 10 to the integer value then the value that is actually stored (as 4 decimal octets) is 0 0 0 10. Note that 10 is an ASCII NL so the output is a newline and again, because this location in memory happens to be followed by NUL's (by accident, more or less) the string output behaves itself.

I have a better idea. Just before your first cout, make this assignment.

var.a = 0x41424300;
or
var.a = 0x31323300;

The explanation for the behavior is left as a student exercise (and the results could vary depending upon the platform, think Big-endian vs. Little-endian).
If it ain't broke, I can fix that.
A. Clay Stephenson
Acclaimed Contributor

Re: Typecasting in C++

And actually, in the case of HP-UX because
of its endianess, decimal 10 is encoded as 0x0000000A and because the first byte printed as a string is 0x00 (NUL) then the output is null.
If it ain't broke, I can fix that.
Stephen Keane
Honored Contributor

Re: Typecasting in C++

What you need to do is tell the C++ compiler how to output a type "test" as it only knows about inbuilt types. So something like ...


#include
using namespace std;

typedef struct
{
int a;
} test;

ostream& operator<<(ostream& os, const test& arg)
{
return(os << arg.a);
}

int main()
{
test var;

var.a = 10;

cout << var << endl;

return(0);
}