<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Typecasting in C++ in Operating System - Linux</title>
    <link>https://community.hpe.com/t5/operating-system-linux/typecasting-in-c/m-p/3793324#M99673</link>
    <description>Hi friends,&lt;BR /&gt;&lt;BR /&gt;I have a problem in type conversion of a structure to character pointer type.&lt;BR /&gt;&lt;BR /&gt;struct{&lt;BR /&gt;int a;&lt;BR /&gt;}test var;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;cout&amp;lt;&amp;lt;(void*)var; /*Gives me the value*/&lt;BR /&gt;&lt;BR /&gt;But if i do like this,&lt;BR /&gt;&lt;BR /&gt;cout&amp;lt;&amp;lt;(char *)var; &lt;BR /&gt;&lt;BR /&gt;I get nothing. What might be the problem?&lt;BR /&gt;Please give me a solution for this at the earliest.&lt;BR /&gt;&lt;BR /&gt;Thanks &amp;amp; Best regards,&lt;BR /&gt;Rajesh P</description>
    <pubDate>Tue, 23 May 2006 10:32:38 GMT</pubDate>
    <dc:creator>Rajesh Parappil</dc:creator>
    <dc:date>2006-05-23T10:32:38Z</dc:date>
    <item>
      <title>Typecasting in C++</title>
      <link>https://community.hpe.com/t5/operating-system-linux/typecasting-in-c/m-p/3793324#M99673</link>
      <description>Hi friends,&lt;BR /&gt;&lt;BR /&gt;I have a problem in type conversion of a structure to character pointer type.&lt;BR /&gt;&lt;BR /&gt;struct{&lt;BR /&gt;int a;&lt;BR /&gt;}test var;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;cout&amp;lt;&amp;lt;(void*)var; /*Gives me the value*/&lt;BR /&gt;&lt;BR /&gt;But if i do like this,&lt;BR /&gt;&lt;BR /&gt;cout&amp;lt;&amp;lt;(char *)var; &lt;BR /&gt;&lt;BR /&gt;I get nothing. What might be the problem?&lt;BR /&gt;Please give me a solution for this at the earliest.&lt;BR /&gt;&lt;BR /&gt;Thanks &amp;amp; Best regards,&lt;BR /&gt;Rajesh P</description>
      <pubDate>Tue, 23 May 2006 10:32:38 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/typecasting-in-c/m-p/3793324#M99673</guid>
      <dc:creator>Rajesh Parappil</dc:creator>
      <dc:date>2006-05-23T10:32:38Z</dc:date>
    </item>
    <item>
      <title>Re: Typecasting in C++</title>
      <link>https://community.hpe.com/t5/operating-system-linux/typecasting-in-c/m-p/3793325#M99674</link>
      <description>First of all, your example is bad syntax so it won't actually compile.&lt;BR /&gt;&lt;BR /&gt;I suppose you actually mean something like this:&lt;BR /&gt;&lt;BR /&gt;cout &amp;lt;&amp;lt; (void *) &amp;amp;var &amp;lt;&amp;lt; endl;&lt;BR /&gt;cout &amp;lt;&amp;lt; (char *) &amp;amp;var &amp;lt;&amp;lt; endl;&lt;BR /&gt;&lt;BR /&gt;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.&lt;BR /&gt;</description>
      <pubDate>Tue, 23 May 2006 10:54:00 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/typecasting-in-c/m-p/3793325#M99674</guid>
      <dc:creator>A. Clay Stephenson</dc:creator>
      <dc:date>2006-05-23T10:54:00Z</dc:date>
    </item>
    <item>
      <title>Re: Typecasting in C++</title>
      <link>https://community.hpe.com/t5/operating-system-linux/typecasting-in-c/m-p/3793326#M99675</link>
      <description>First of all thanks for giving me a reply.&lt;BR /&gt;&lt;BR /&gt;   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??&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Tue, 23 May 2006 11:15:12 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/typecasting-in-c/m-p/3793326#M99675</guid>
      <dc:creator>Rajesh Parappil</dc:creator>
      <dc:date>2006-05-23T11:15:12Z</dc:date>
    </item>
    <item>
      <title>Re: Typecasting in C++</title>
      <link>https://community.hpe.com/t5/operating-system-linux/typecasting-in-c/m-p/3793327#M99676</link>
      <description>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.</description>
      <pubDate>Tue, 23 May 2006 11:23:12 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/typecasting-in-c/m-p/3793327#M99676</guid>
      <dc:creator>A. Clay Stephenson</dc:creator>
      <dc:date>2006-05-23T11:23:12Z</dc:date>
    </item>
    <item>
      <title>Re: Typecasting in C++</title>
      <link>https://community.hpe.com/t5/operating-system-linux/typecasting-in-c/m-p/3793328#M99677</link>
      <description>Thanks for your reply.&lt;BR /&gt;&lt;BR /&gt;Here my structure variable is declared in a function and before printing the pointer, I have assigned a value to the variable.&lt;BR /&gt;&lt;BR /&gt;var.a = 10;&lt;BR /&gt;&lt;BR /&gt;I think that eliminates the possibility of the variable "var" being a garbage. Please correct me if I am wrong.&lt;BR /&gt;&lt;BR /&gt;Do you think it has to do anything with type casting?&lt;BR /&gt;&lt;BR /&gt;Interestingly this is a copy paste code and was working in some other application.&lt;BR /&gt;</description>
      <pubDate>Tue, 23 May 2006 11:36:11 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/typecasting-in-c/m-p/3793328#M99677</guid>
      <dc:creator>Rajesh Parappil</dc:creator>
      <dc:date>2006-05-23T11:36:11Z</dc:date>
    </item>
    <item>
      <title>Re: Typecasting in C++</title>
      <link>https://community.hpe.com/t5/operating-system-linux/typecasting-in-c/m-p/3793329#M99678</link>
      <description>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.&lt;BR /&gt;&lt;BR /&gt;I have a better idea. Just before your first cout, make this assignment.&lt;BR /&gt;&lt;BR /&gt;var.a = 0x41424300;&lt;BR /&gt;or&lt;BR /&gt;var.a = 0x31323300;&lt;BR /&gt;&lt;BR /&gt;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).</description>
      <pubDate>Tue, 23 May 2006 11:49:18 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/typecasting-in-c/m-p/3793329#M99678</guid>
      <dc:creator>A. Clay Stephenson</dc:creator>
      <dc:date>2006-05-23T11:49:18Z</dc:date>
    </item>
    <item>
      <title>Re: Typecasting in C++</title>
      <link>https://community.hpe.com/t5/operating-system-linux/typecasting-in-c/m-p/3793330#M99679</link>
      <description>And actually, in the case of HP-UX because&lt;BR /&gt;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.</description>
      <pubDate>Tue, 23 May 2006 11:55:29 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/typecasting-in-c/m-p/3793330#M99679</guid>
      <dc:creator>A. Clay Stephenson</dc:creator>
      <dc:date>2006-05-23T11:55:29Z</dc:date>
    </item>
    <item>
      <title>Re: Typecasting in C++</title>
      <link>https://community.hpe.com/t5/operating-system-linux/typecasting-in-c/m-p/3793331#M99680</link>
      <description>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 ...&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;#include &lt;IOSTREAM&gt;&lt;BR /&gt;using namespace std;&lt;BR /&gt;&lt;BR /&gt;typedef struct&lt;BR /&gt;{&lt;BR /&gt;   int a;&lt;BR /&gt;}  test;&lt;BR /&gt;&lt;BR /&gt;ostream&amp;amp; operator&amp;lt;&amp;lt;(ostream&amp;amp; os, const test&amp;amp; arg)&lt;BR /&gt;{&lt;BR /&gt;    return(os &amp;lt;&amp;lt; arg.a);&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;int main()&lt;BR /&gt;{&lt;BR /&gt;    test var;&lt;BR /&gt;&lt;BR /&gt;    var.a = 10;&lt;BR /&gt;&lt;BR /&gt;    cout &amp;lt;&amp;lt; var &amp;lt;&amp;lt; endl;&lt;BR /&gt;&lt;BR /&gt;    return(0);&lt;BR /&gt;}&lt;BR /&gt;&lt;/IOSTREAM&gt;</description>
      <pubDate>Wed, 24 May 2006 04:29:39 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/typecasting-in-c/m-p/3793331#M99680</guid>
      <dc:creator>Stephen Keane</dc:creator>
      <dc:date>2006-05-24T04:29:39Z</dc:date>
    </item>
  </channel>
</rss>

