- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Typecasting in C++
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-23-2006 03:32 AM
05-23-2006 03:32 AM
Typecasting in C++
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
- Tags:
- iostream
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-23-2006 03:54 AM
05-23-2006 03:54 AM
Re: Typecasting in C++
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-23-2006 04:15 AM
05-23-2006 04:15 AM
Re: Typecasting in C++
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??
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-23-2006 04:23 AM
05-23-2006 04:23 AM
Re: Typecasting in C++
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-23-2006 04:36 AM
05-23-2006 04:36 AM
Re: Typecasting in C++
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-23-2006 04:49 AM
05-23-2006 04:49 AM
Re: Typecasting in C++
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).
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-23-2006 04:55 AM
05-23-2006 04:55 AM
Re: Typecasting in C++
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-23-2006 09:29 PM
05-23-2006 09:29 PM
Re: Typecasting in C++
#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);
}