<?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 c syntax: pointer type cast in ColdFire/68K Microcontrollers and Processors</title>
    <link>https://community.nxp.com/t5/ColdFire-68K-Microcontrollers/c-syntax-pointer-type-cast/m-p/130832#M772</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;DIV&gt; &lt;/DIV&gt;&lt;DIV&gt; &lt;/DIV&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;SPAN style="color: #ff0000;"&gt;This message contains an entire topic ported&amp;nbsp;from the WildRice - Coldfire forum.&amp;nbsp; Freescale has received the approval from the WildRice administrator on seeding the Freescale forum with messages.&amp;nbsp; The original message and all replies are in this single message. We have seeded this new forum with selected information that we expect will be of value as you search for answers to your questions.&amp;nbsp;&lt;/SPAN&gt; &lt;SPAN style="color: #ff0000;"&gt;Freescale assumes no responsibility whatsoever with respect to Posted Material.&amp;nbsp; For additional information, please see the &lt;A _jive_internal="true" href="https://community.nxp.com/external-link.jspa?url=http%3A%2F%2Fwww.freescale.com%2Ffiles%2Fabstract%2Fhelp_page%2FTERMSOFUSE.html" rel="nofollow" target="_blank"&gt;&lt;SPAN style="color: #000000;"&gt;Terms of Use - Message Boards and Community Forums&lt;/SPAN&gt;&lt;/A&gt;.&amp;nbsp; Thank You and Enjoy the Forum!&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;BR /&gt;&lt;HR /&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;DIV&gt;Jan 21, 2006, 6:19 AM&lt;/DIV&gt;&lt;DIV&gt;Post #1 of 4 (82 views)&lt;BR /&gt;Copy Shortcut&lt;BR /&gt;&amp;nbsp;[ColdFire] c syntax: pointer type cast&amp;nbsp; Can't Post&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;--------------------------------------------------------------------------------&lt;BR /&gt;&amp;nbsp;&lt;BR /&gt;let us assume i utilize "fake" long variables, indeed pointing to data.&lt;BR /&gt;(i do not have access to c compiler at every system/it crashes on some windows versions by random)&lt;/DIV&gt;&lt;DIV&gt;long src_data;&lt;BR /&gt;long tags;&lt;BR /&gt;long dst_buffer;&lt;/DIV&gt;&lt;DIV&gt;now i would like to compare two longwords:&lt;BR /&gt;if( (*(UBYTE *)src_data)==(*(UBYTE *)tags) ) {do_something();}&lt;/DIV&gt;&lt;DIV&gt;then i store a result:&lt;BR /&gt;(*(ULONG *)dst_buffer)=data;&lt;/DIV&gt;&lt;DIV&gt;and then i go to the next longword data item:&lt;BR /&gt;(ULONG *)dst_buffer++;&lt;/DIV&gt;&lt;DIV&gt;or the datasize gets added:&lt;BR /&gt;dst_buffer+=sizeof(struct Image); /* size of data structure */&lt;BR /&gt;dst_buffer=alignchunky(dst_buffer); /* align to next 16byte boundary */&lt;/DIV&gt;&lt;DIV&gt;it's the easiest way, because i utilize arrays pointing to memory area's.&lt;BR /&gt;in example while they are initialized, different data gets copied in, and each time the data buffer gets padded to 16byte boundary. most effectively i do it by just accessing the pointer as "long" variable, or sometimes casting it to "long variable pointer" (ULONG *).&lt;/DIV&gt;&lt;DIV&gt;now my question: are the above c expressions correct, or can i write them in a better/more simple form?&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;BR /&gt;---------------------------------&lt;BR /&gt;&lt;/DIV&gt;&lt;DIV&gt;Jan 22, 2006, 3:49 PM&lt;/DIV&gt;&lt;DIV&gt;Post #2 of 4 (78 views)&lt;BR /&gt;Copy Shortcut&lt;BR /&gt;&amp;nbsp;Re: [ColdFire] c syntax: pointer type cast [In reply to]&amp;nbsp; Can't Post&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;--------------------------------------------------------------------------------&lt;BR /&gt;&amp;nbsp;&lt;BR /&gt;alex wrote:&lt;BR /&gt;&amp;gt; let us assume i utilize "fake" long variables, indeed pointing to&lt;BR /&gt;&amp;gt; data. (i do not have access to c compiler at every system/it crashes&lt;BR /&gt;&amp;gt; on some windows versions by random)&lt;BR /&gt;&amp;gt;&lt;BR /&gt;&amp;gt; long src_data; long tags; long dst_buffer;&lt;BR /&gt;&amp;gt;&lt;BR /&gt;&amp;gt; now i would like to compare two longwords: if( (*(UBYTE&lt;BR /&gt;&amp;gt; *)src_data)==(*(UBYTE *)tags) ) {do_something();}&lt;/DIV&gt;&lt;DIV&gt;That statement is comparing two bytes.&lt;BR /&gt;&amp;gt;&lt;BR /&gt;&amp;gt; then i store a result: (*(ULONG *)dst_buffer)=data;&lt;BR /&gt;&amp;gt;&lt;BR /&gt;&amp;gt; and then i go to the next longword data item: (ULONG *)dst_buffer++;&lt;/DIV&gt;&lt;DIV&gt;So far so good... but the casting is pretty ugly!&lt;/DIV&gt;&lt;DIV&gt;If you have access to it on your system I would&lt;BR /&gt;recommend the new C99 types uint8_t and uint32_t&lt;/DIV&gt;&lt;DIV&gt;&amp;gt; or the datasize gets added: dst_buffer+=sizeof(struct Image); /*&lt;BR /&gt;&amp;gt; size of data structure */&lt;/DIV&gt;&lt;DIV&gt;WRONG!!!...&lt;BR /&gt;this will increment dst_buffer by&lt;BR /&gt;sizeof(long) * sizeof(struct Image )&lt;/DIV&gt;&lt;DIV&gt;&lt;BR /&gt;&amp;gt; dst_buffer=alignchunky(dst_buffer); /* align to next 16byte boundary&lt;BR /&gt;&amp;gt; */&lt;/DIV&gt;&lt;DIV&gt;As above.&lt;/DIV&gt;&lt;DIV&gt;&amp;gt; it's the easiest way, because i utilize arrays pointing to memory&lt;BR /&gt;&amp;gt; area's. in example while they are initialized, different data gets&lt;BR /&gt;&amp;gt; copied in, and each time the data buffer gets padded to 16byte&lt;BR /&gt;&amp;gt; boundary. most effectively i do it by just accessing the pointer as&lt;BR /&gt;&amp;gt; "long" variable, or sometimes casting it to "long variable pointer"&lt;BR /&gt;&amp;gt; (ULONG *).&lt;/DIV&gt;&lt;DIV&gt;I would do it like this (but tastes do vary).&lt;/DIV&gt;&lt;DIV&gt;uint32_t *dst;&lt;BR /&gt;uint32_t *src;&lt;BR /&gt;uint8_t * u8dst = (uint8_t * ) dst;&lt;BR /&gt;uint8_t * u8src = (uint8_t * ) src;&lt;/DIV&gt;&lt;DIV&gt;if ( *u8dst == *u8src ) do_something();&lt;/DIV&gt;&lt;DIV&gt;if you want to increment the long pointer by the sizeof a struct&lt;BR /&gt;you need to be careful.&lt;/DIV&gt;&lt;DIV&gt;dst += sizeof(struct Image ) / sizeof(uint32_t ) ;&lt;/DIV&gt;&lt;DIV&gt;Regards&lt;/DIV&gt;&lt;DIV&gt;Paul&lt;/DIV&gt;&lt;DIV&gt;=========================================================================&lt;/DIV&gt;&lt;DIV&gt;Jan 23, 2006, 1:53 AM&lt;/DIV&gt;&lt;DIV&gt;Post #3 of 4 (78 views)&lt;BR /&gt;Copy Shortcut&lt;BR /&gt;&amp;nbsp;Re: [ColdFire] c syntax: pointer type cast [In reply to]&amp;nbsp; Can't Post&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;--------------------------------------------------------------------------------&lt;BR /&gt;&amp;nbsp;&lt;BR /&gt;Paul Whitfield wrote: alex wrote:&lt;BR /&gt;&amp;gt; let us assume i utilize "fake" long variables, indeed pointing to&lt;BR /&gt;&amp;gt; data. (i do not have access to c compiler at every system/it crashes&lt;BR /&gt;&amp;gt; on some windows versions by random)&lt;BR /&gt;&amp;gt;&lt;BR /&gt;&amp;gt; long src_data; long tags; long dst_buffer;&lt;BR /&gt;&amp;gt;&lt;BR /&gt;&amp;gt; now i would like to compare two longwords: if( (*(UBYTE&lt;BR /&gt;&amp;gt; *)src_data)==(*(UBYTE *)tags) ) {do_something();}&lt;/DIV&gt;&lt;DIV&gt;That statement is comparing two bytes.&lt;BR /&gt;&amp;gt;&lt;BR /&gt;&amp;gt; then i store a result: (*(ULONG *)dst_buffer)=data;&lt;BR /&gt;&amp;gt;&lt;BR /&gt;&amp;gt; and then i go to the next longword data item: (ULONG *)dst_buffer++;&lt;/DIV&gt;&lt;DIV&gt;So far so good... but the casting is pretty ugly!&lt;/DIV&gt;&lt;DIV&gt;If you have access to it on your system I would&lt;BR /&gt;recommend the new C99 types uint8_t and uint32_t&lt;/DIV&gt;&lt;DIV&gt;&amp;gt; or the datasize gets added: dst_buffer+=sizeof(struct Image); /*&lt;BR /&gt;&amp;gt; size of data structure */&lt;/DIV&gt;&lt;DIV&gt;WRONG!!!...&lt;BR /&gt;this will increment dst_buffer by&lt;BR /&gt;sizeof(long) * sizeof(struct Image )&lt;/DIV&gt;&lt;DIV&gt;&lt;BR /&gt;&amp;gt; dst_buffer=alignchunky(dst_buffer); /* align to next 16byte boundary&lt;BR /&gt;&amp;gt; */&lt;/DIV&gt;&lt;DIV&gt;As above.&lt;/DIV&gt;&lt;DIV&gt;&amp;gt; it's the easiest way, because i utilize arrays pointing to memory&lt;BR /&gt;&amp;gt; area's. in example while they are initialized, different data gets&lt;BR /&gt;&amp;gt; copied in, and each time the data buffer gets padded to 16byte&lt;BR /&gt;&amp;gt; boundary. most effectively i do it by just accessing the pointer as&lt;BR /&gt;&amp;gt; "long" variable, or sometimes casting it to "long variable pointer"&lt;BR /&gt;&amp;gt; (ULONG *).&lt;/DIV&gt;&lt;DIV&gt;I would do it like this (but tastes do vary).&lt;/DIV&gt;&lt;DIV&gt;uint32_t *dst;&lt;BR /&gt;uint32_t *src;&lt;BR /&gt;uint8_t * u8dst = (uint8_t * ) dst;&lt;BR /&gt;uint8_t * u8src = (uint8_t * ) src;&lt;/DIV&gt;&lt;DIV&gt;if ( *u8dst == *u8src ) do_something();&lt;/DIV&gt;&lt;DIV&gt;if you want to increment the long pointer by the sizeof a struct&lt;BR /&gt;you need to be careful.&lt;/DIV&gt;&lt;DIV&gt;dst += sizeof(struct Image ) / sizeof(uint32_t ) ;&lt;/DIV&gt;&lt;DIV&gt;Regards&lt;/DIV&gt;&lt;DIV&gt;Paul&lt;/DIV&gt;&lt;DIV&gt;=========================================================================&lt;BR /&gt;The information in this e-mail is intended for the addressee only.&lt;BR /&gt;Unauthorised use, copying, disclosure or distribution by anyone else&lt;BR /&gt;is prohibited. Please let us know immediately if you receive this&lt;BR /&gt;e-mail in error. Thank you.&lt;BR /&gt;=========================================================================&lt;/DIV&gt;&lt;DIV&gt;--------------------------------------------------------------------&lt;/DIV&gt;&lt;DIV&gt;&lt;BR /&gt;(UBYTE *)chipmembuffer2+=dsize; /* long chipmembuffer2 */&lt;BR /&gt;this works (it compiles, links and executes)&lt;BR /&gt;thought i can leave it out (the c compiler does not know its a pointer)&lt;/DIV&gt;&lt;DIV&gt;by the way its a pretty old one. what happens if i compile for ms-dos/windows?&lt;BR /&gt;here i store linear 32bit address into long, and treat it as pointer (by doing typecast). anyway its not executeable under ms-dos (addressed hardware not existent), just asking if a c compiler generates a (water resistent) segment&lt;A href="http://freescale.i.lithium.com/i/smilies/16x16_smiley-surprised.gif"&gt;&lt;IMG alt=":smileysurprised:" class="emoticon emoticon-smileysurprised" src="http://freescale.i.lithium.com/i/smilies/16x16_smiley-surprised.gif" title="Smiley Surprised" /&gt;&lt;/A&gt;ffset addressing layer. (like borland pascal always normalizes to "segment only" pointers by own device, but accepts changes i.e. add to the offset). i assume each time i cast to a pointer type the compiler generates an addressing layer (which may vary from system to system)&lt;/DIV&gt;&lt;DIV&gt;now the reason for NOT using a derived datatype: the values are freely passed around to various functions, stored into data structures for later processing. then i would need to cast to long, and would need to take care to pass the right datatype to functions. probably its oldfashioned to treat pointers just as "long" values.&lt;BR /&gt;(alignchunky() makes sense for any memory address, it does not depend on the datatype. the system requires "even" alignment, and even "longword" alignment for some I/O data structures. in this case i need to declare the datatype, and to de-cast it each time i pass it to functions like alignchunky().)&lt;/DIV&gt;&lt;DIV&gt;one answer i found myself: not to mix various addressing methods/cast systems. even if the code size increases and execution slows down.&lt;/DIV&gt;&lt;DIV&gt;alex finn, dublin, ireland. electronic games and effective design.&lt;BR /&gt;information about superstition: &lt;A _jive_internal="true" href="https://community.nxp.com/external-link.jspa?url=http%3A%2F%2Fuk.geocities.com%2Fnikemoto2511" rel="nofollow" target="_blank"&gt;http://uk.geocities.com/nikemoto2511&lt;/A&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Jan 24, 2006, 1:42 AM&lt;/DIV&gt;&lt;DIV&gt;Post #4 of 4 (76 views)&lt;BR /&gt;Copy Shortcut&lt;BR /&gt;&amp;nbsp;Re: [ColdFire] c syntax: pointer type cast [In reply to]&amp;nbsp; Can't Post&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;--------------------------------------------------------------------------------&lt;BR /&gt;&amp;nbsp;&lt;BR /&gt;regards, however i have put in "long" for illustration purpose.&lt;BR /&gt;lthe way you state it: that's how it WAS.&lt;BR /&gt;but i can see now i labelled/commented it wrong:&lt;/DIV&gt;&lt;DIV&gt;&amp;gt;now i would like to compare two UBYTE values:&lt;BR /&gt;&amp;gt; if( (*(UBYTE *)src_data)==(*(UBYTE *)tags) ) {do_something();}&lt;/DIV&gt;&lt;DIV&gt;if it does not work correctly (inc/dec numeric variable), i need to add additional&lt;BR /&gt;casts (they do work, i have tested it). some documents explain that "pointer variables get increased by the datasize". however its difficult/interruptive for me to compile/test/debug it right now, hence i ask...&lt;/DIV&gt;&lt;DIV&gt;the background are arrays of pointers to structures which: contain pointers to others structures, memory areas and so on. it just does not make sense to make up highly specialized datatypes/pointer types, and then downcast them just to add a numerical value.&lt;/DIV&gt;&lt;DIV&gt;if an address (pointer=address representation) gets aligned to EVEN, 4byte,... it has nothing to do with the type of data, its just a numerical thing.&lt;/DIV&gt;&lt;DIV&gt;nikemoto2511&lt;/DIV&gt;&lt;DIV&gt;--------------------------------------------------------------------------------------------------------------&lt;/DIV&gt;&lt;DIV&gt;&lt;BR /&gt;Scott Hauck &amp;lt;&lt;A class="jive-link-email-small" href="mailto:haucks@nextnetwireless.com"&gt;haucks@nextnetwireless.com&lt;/A&gt;&amp;gt; wrote:&lt;BR /&gt;I see someone already replied to you, but I have just a couple more points.&lt;/DIV&gt;&lt;DIV&gt;&amp;gt; now i would like to compare two longwords:&lt;BR /&gt;&amp;gt; if( (*(UBYTE *)src_data)==(*(UBYTE *)tags) ) {do_something();}&lt;/DIV&gt;&lt;DIV&gt;src_data and tags are already of type long so if you wish&lt;BR /&gt;to compare longwords, then "(src_data == tags)" would do it&lt;BR /&gt;for you. The statement you have isn't just a byte compare,&lt;BR /&gt;but you are casting src_data and tags as pointers to bytes.&lt;BR /&gt;Therefore you statement is comparing the byte information&lt;BR /&gt;that is being pointed at by src_data and tags. Probably not&lt;BR /&gt;what you want...&lt;/DIV&gt;&lt;DIV&gt;&amp;gt; then i store a result:&lt;BR /&gt;&amp;gt; (*(ULONG *)dst_buffer)=data;&lt;/DIV&gt;&lt;DIV&gt;Another pointer problem here. dst_buffer is a long and you&lt;BR /&gt;have cast it to a pointer to an 'unsigned long'. So when you&lt;BR /&gt;store the data, you are actually storing it in a location&lt;BR /&gt;pointed to by dst_buffer.&lt;/DIV&gt;&lt;DIV&gt;&amp;gt; and then i go to the next longword data item:&lt;BR /&gt;&amp;gt; (ULONG *)dst_buffer++;&lt;/DIV&gt;&lt;DIV&gt;You don't need the cast. Just "dst_buffer++" is sufficient.&lt;/DIV&gt;&lt;DIV&gt;&amp;gt; or the datasize gets added:&lt;BR /&gt;&amp;gt; dst_buffer+=sizeof(struct Image); /* size of data structure */&lt;/DIV&gt;&lt;DIV&gt;The other responder was correct. When you increment a variable&lt;BR /&gt;it gets incremented by the size of its type. Likewise, when&lt;BR /&gt;adding to a variable, it gets whatever is being added *times*&lt;BR /&gt;the size of the receiving variable.&lt;/DIV&gt;&lt;DIV&gt;Pointers are a somewhat confusing element of C. In fact,&lt;BR /&gt;it is hardly even taught in school anymore. If you truly&lt;BR /&gt;want these things to be pointers, then declare them that way&lt;BR /&gt;instead of constantly having to cast them.&lt;/DIV&gt;&lt;DIV&gt;long *src_data;&lt;BR /&gt;long *tags;&lt;BR /&gt;long *dst_buffer;&lt;/DIV&gt;&lt;DIV&gt;# compare 2 longwords&lt;BR /&gt;if (*src_data == *dst_buffer) {do_something();}&lt;/DIV&gt;&lt;DIV&gt;# then store a result:&lt;BR /&gt;*dst_buffer = data;&lt;/DIV&gt;&lt;DIV&gt;# and then i go to the next longword data item:&lt;BR /&gt;dst_buffer++;&lt;/DIV&gt;&lt;DIV&gt;&lt;BR /&gt;-Scott&lt;/DIV&gt;&lt;DIV&gt;. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .&lt;/DIV&gt;&lt;DIV&gt;&lt;BR /&gt;&amp;nbsp;&lt;BR /&gt;&amp;nbsp;&lt;BR /&gt;&amp;nbsp;&lt;BR /&gt;&lt;/DIV&gt;&lt;P&gt;Message Edited by Dietrich on &lt;SPAN class="date_text"&gt;04-01-2006&lt;/SPAN&gt; &lt;SPAN class="time_text"&gt;11:34 AM&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;Message Edited by Dietrich on &lt;SPAN class="date_text"&gt;04-04-2006&lt;/SPAN&gt; &lt;SPAN class="time_text"&gt;09:33 PM&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 01 Apr 2006 06:30:24 GMT</pubDate>
    <dc:creator>Dietrich</dc:creator>
    <dc:date>2006-04-01T06:30:24Z</dc:date>
    <item>
      <title>c syntax: pointer type cast</title>
      <link>https://community.nxp.com/t5/ColdFire-68K-Microcontrollers/c-syntax-pointer-type-cast/m-p/130832#M772</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;DIV&gt; &lt;/DIV&gt;&lt;DIV&gt; &lt;/DIV&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;SPAN style="color: #ff0000;"&gt;This message contains an entire topic ported&amp;nbsp;from the WildRice - Coldfire forum.&amp;nbsp; Freescale has received the approval from the WildRice administrator on seeding the Freescale forum with messages.&amp;nbsp; The original message and all replies are in this single message. We have seeded this new forum with selected information that we expect will be of value as you search for answers to your questions.&amp;nbsp;&lt;/SPAN&gt; &lt;SPAN style="color: #ff0000;"&gt;Freescale assumes no responsibility whatsoever with respect to Posted Material.&amp;nbsp; For additional information, please see the &lt;A _jive_internal="true" href="https://community.nxp.com/external-link.jspa?url=http%3A%2F%2Fwww.freescale.com%2Ffiles%2Fabstract%2Fhelp_page%2FTERMSOFUSE.html" rel="nofollow" target="_blank"&gt;&lt;SPAN style="color: #000000;"&gt;Terms of Use - Message Boards and Community Forums&lt;/SPAN&gt;&lt;/A&gt;.&amp;nbsp; Thank You and Enjoy the Forum!&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;BR /&gt;&lt;HR /&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;DIV&gt;Jan 21, 2006, 6:19 AM&lt;/DIV&gt;&lt;DIV&gt;Post #1 of 4 (82 views)&lt;BR /&gt;Copy Shortcut&lt;BR /&gt;&amp;nbsp;[ColdFire] c syntax: pointer type cast&amp;nbsp; Can't Post&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;--------------------------------------------------------------------------------&lt;BR /&gt;&amp;nbsp;&lt;BR /&gt;let us assume i utilize "fake" long variables, indeed pointing to data.&lt;BR /&gt;(i do not have access to c compiler at every system/it crashes on some windows versions by random)&lt;/DIV&gt;&lt;DIV&gt;long src_data;&lt;BR /&gt;long tags;&lt;BR /&gt;long dst_buffer;&lt;/DIV&gt;&lt;DIV&gt;now i would like to compare two longwords:&lt;BR /&gt;if( (*(UBYTE *)src_data)==(*(UBYTE *)tags) ) {do_something();}&lt;/DIV&gt;&lt;DIV&gt;then i store a result:&lt;BR /&gt;(*(ULONG *)dst_buffer)=data;&lt;/DIV&gt;&lt;DIV&gt;and then i go to the next longword data item:&lt;BR /&gt;(ULONG *)dst_buffer++;&lt;/DIV&gt;&lt;DIV&gt;or the datasize gets added:&lt;BR /&gt;dst_buffer+=sizeof(struct Image); /* size of data structure */&lt;BR /&gt;dst_buffer=alignchunky(dst_buffer); /* align to next 16byte boundary */&lt;/DIV&gt;&lt;DIV&gt;it's the easiest way, because i utilize arrays pointing to memory area's.&lt;BR /&gt;in example while they are initialized, different data gets copied in, and each time the data buffer gets padded to 16byte boundary. most effectively i do it by just accessing the pointer as "long" variable, or sometimes casting it to "long variable pointer" (ULONG *).&lt;/DIV&gt;&lt;DIV&gt;now my question: are the above c expressions correct, or can i write them in a better/more simple form?&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;BR /&gt;---------------------------------&lt;BR /&gt;&lt;/DIV&gt;&lt;DIV&gt;Jan 22, 2006, 3:49 PM&lt;/DIV&gt;&lt;DIV&gt;Post #2 of 4 (78 views)&lt;BR /&gt;Copy Shortcut&lt;BR /&gt;&amp;nbsp;Re: [ColdFire] c syntax: pointer type cast [In reply to]&amp;nbsp; Can't Post&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;--------------------------------------------------------------------------------&lt;BR /&gt;&amp;nbsp;&lt;BR /&gt;alex wrote:&lt;BR /&gt;&amp;gt; let us assume i utilize "fake" long variables, indeed pointing to&lt;BR /&gt;&amp;gt; data. (i do not have access to c compiler at every system/it crashes&lt;BR /&gt;&amp;gt; on some windows versions by random)&lt;BR /&gt;&amp;gt;&lt;BR /&gt;&amp;gt; long src_data; long tags; long dst_buffer;&lt;BR /&gt;&amp;gt;&lt;BR /&gt;&amp;gt; now i would like to compare two longwords: if( (*(UBYTE&lt;BR /&gt;&amp;gt; *)src_data)==(*(UBYTE *)tags) ) {do_something();}&lt;/DIV&gt;&lt;DIV&gt;That statement is comparing two bytes.&lt;BR /&gt;&amp;gt;&lt;BR /&gt;&amp;gt; then i store a result: (*(ULONG *)dst_buffer)=data;&lt;BR /&gt;&amp;gt;&lt;BR /&gt;&amp;gt; and then i go to the next longword data item: (ULONG *)dst_buffer++;&lt;/DIV&gt;&lt;DIV&gt;So far so good... but the casting is pretty ugly!&lt;/DIV&gt;&lt;DIV&gt;If you have access to it on your system I would&lt;BR /&gt;recommend the new C99 types uint8_t and uint32_t&lt;/DIV&gt;&lt;DIV&gt;&amp;gt; or the datasize gets added: dst_buffer+=sizeof(struct Image); /*&lt;BR /&gt;&amp;gt; size of data structure */&lt;/DIV&gt;&lt;DIV&gt;WRONG!!!...&lt;BR /&gt;this will increment dst_buffer by&lt;BR /&gt;sizeof(long) * sizeof(struct Image )&lt;/DIV&gt;&lt;DIV&gt;&lt;BR /&gt;&amp;gt; dst_buffer=alignchunky(dst_buffer); /* align to next 16byte boundary&lt;BR /&gt;&amp;gt; */&lt;/DIV&gt;&lt;DIV&gt;As above.&lt;/DIV&gt;&lt;DIV&gt;&amp;gt; it's the easiest way, because i utilize arrays pointing to memory&lt;BR /&gt;&amp;gt; area's. in example while they are initialized, different data gets&lt;BR /&gt;&amp;gt; copied in, and each time the data buffer gets padded to 16byte&lt;BR /&gt;&amp;gt; boundary. most effectively i do it by just accessing the pointer as&lt;BR /&gt;&amp;gt; "long" variable, or sometimes casting it to "long variable pointer"&lt;BR /&gt;&amp;gt; (ULONG *).&lt;/DIV&gt;&lt;DIV&gt;I would do it like this (but tastes do vary).&lt;/DIV&gt;&lt;DIV&gt;uint32_t *dst;&lt;BR /&gt;uint32_t *src;&lt;BR /&gt;uint8_t * u8dst = (uint8_t * ) dst;&lt;BR /&gt;uint8_t * u8src = (uint8_t * ) src;&lt;/DIV&gt;&lt;DIV&gt;if ( *u8dst == *u8src ) do_something();&lt;/DIV&gt;&lt;DIV&gt;if you want to increment the long pointer by the sizeof a struct&lt;BR /&gt;you need to be careful.&lt;/DIV&gt;&lt;DIV&gt;dst += sizeof(struct Image ) / sizeof(uint32_t ) ;&lt;/DIV&gt;&lt;DIV&gt;Regards&lt;/DIV&gt;&lt;DIV&gt;Paul&lt;/DIV&gt;&lt;DIV&gt;=========================================================================&lt;/DIV&gt;&lt;DIV&gt;Jan 23, 2006, 1:53 AM&lt;/DIV&gt;&lt;DIV&gt;Post #3 of 4 (78 views)&lt;BR /&gt;Copy Shortcut&lt;BR /&gt;&amp;nbsp;Re: [ColdFire] c syntax: pointer type cast [In reply to]&amp;nbsp; Can't Post&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;--------------------------------------------------------------------------------&lt;BR /&gt;&amp;nbsp;&lt;BR /&gt;Paul Whitfield wrote: alex wrote:&lt;BR /&gt;&amp;gt; let us assume i utilize "fake" long variables, indeed pointing to&lt;BR /&gt;&amp;gt; data. (i do not have access to c compiler at every system/it crashes&lt;BR /&gt;&amp;gt; on some windows versions by random)&lt;BR /&gt;&amp;gt;&lt;BR /&gt;&amp;gt; long src_data; long tags; long dst_buffer;&lt;BR /&gt;&amp;gt;&lt;BR /&gt;&amp;gt; now i would like to compare two longwords: if( (*(UBYTE&lt;BR /&gt;&amp;gt; *)src_data)==(*(UBYTE *)tags) ) {do_something();}&lt;/DIV&gt;&lt;DIV&gt;That statement is comparing two bytes.&lt;BR /&gt;&amp;gt;&lt;BR /&gt;&amp;gt; then i store a result: (*(ULONG *)dst_buffer)=data;&lt;BR /&gt;&amp;gt;&lt;BR /&gt;&amp;gt; and then i go to the next longword data item: (ULONG *)dst_buffer++;&lt;/DIV&gt;&lt;DIV&gt;So far so good... but the casting is pretty ugly!&lt;/DIV&gt;&lt;DIV&gt;If you have access to it on your system I would&lt;BR /&gt;recommend the new C99 types uint8_t and uint32_t&lt;/DIV&gt;&lt;DIV&gt;&amp;gt; or the datasize gets added: dst_buffer+=sizeof(struct Image); /*&lt;BR /&gt;&amp;gt; size of data structure */&lt;/DIV&gt;&lt;DIV&gt;WRONG!!!...&lt;BR /&gt;this will increment dst_buffer by&lt;BR /&gt;sizeof(long) * sizeof(struct Image )&lt;/DIV&gt;&lt;DIV&gt;&lt;BR /&gt;&amp;gt; dst_buffer=alignchunky(dst_buffer); /* align to next 16byte boundary&lt;BR /&gt;&amp;gt; */&lt;/DIV&gt;&lt;DIV&gt;As above.&lt;/DIV&gt;&lt;DIV&gt;&amp;gt; it's the easiest way, because i utilize arrays pointing to memory&lt;BR /&gt;&amp;gt; area's. in example while they are initialized, different data gets&lt;BR /&gt;&amp;gt; copied in, and each time the data buffer gets padded to 16byte&lt;BR /&gt;&amp;gt; boundary. most effectively i do it by just accessing the pointer as&lt;BR /&gt;&amp;gt; "long" variable, or sometimes casting it to "long variable pointer"&lt;BR /&gt;&amp;gt; (ULONG *).&lt;/DIV&gt;&lt;DIV&gt;I would do it like this (but tastes do vary).&lt;/DIV&gt;&lt;DIV&gt;uint32_t *dst;&lt;BR /&gt;uint32_t *src;&lt;BR /&gt;uint8_t * u8dst = (uint8_t * ) dst;&lt;BR /&gt;uint8_t * u8src = (uint8_t * ) src;&lt;/DIV&gt;&lt;DIV&gt;if ( *u8dst == *u8src ) do_something();&lt;/DIV&gt;&lt;DIV&gt;if you want to increment the long pointer by the sizeof a struct&lt;BR /&gt;you need to be careful.&lt;/DIV&gt;&lt;DIV&gt;dst += sizeof(struct Image ) / sizeof(uint32_t ) ;&lt;/DIV&gt;&lt;DIV&gt;Regards&lt;/DIV&gt;&lt;DIV&gt;Paul&lt;/DIV&gt;&lt;DIV&gt;=========================================================================&lt;BR /&gt;The information in this e-mail is intended for the addressee only.&lt;BR /&gt;Unauthorised use, copying, disclosure or distribution by anyone else&lt;BR /&gt;is prohibited. Please let us know immediately if you receive this&lt;BR /&gt;e-mail in error. Thank you.&lt;BR /&gt;=========================================================================&lt;/DIV&gt;&lt;DIV&gt;--------------------------------------------------------------------&lt;/DIV&gt;&lt;DIV&gt;&lt;BR /&gt;(UBYTE *)chipmembuffer2+=dsize; /* long chipmembuffer2 */&lt;BR /&gt;this works (it compiles, links and executes)&lt;BR /&gt;thought i can leave it out (the c compiler does not know its a pointer)&lt;/DIV&gt;&lt;DIV&gt;by the way its a pretty old one. what happens if i compile for ms-dos/windows?&lt;BR /&gt;here i store linear 32bit address into long, and treat it as pointer (by doing typecast). anyway its not executeable under ms-dos (addressed hardware not existent), just asking if a c compiler generates a (water resistent) segment&lt;A href="http://freescale.i.lithium.com/i/smilies/16x16_smiley-surprised.gif"&gt;&lt;IMG alt=":smileysurprised:" class="emoticon emoticon-smileysurprised" src="http://freescale.i.lithium.com/i/smilies/16x16_smiley-surprised.gif" title="Smiley Surprised" /&gt;&lt;/A&gt;ffset addressing layer. (like borland pascal always normalizes to "segment only" pointers by own device, but accepts changes i.e. add to the offset). i assume each time i cast to a pointer type the compiler generates an addressing layer (which may vary from system to system)&lt;/DIV&gt;&lt;DIV&gt;now the reason for NOT using a derived datatype: the values are freely passed around to various functions, stored into data structures for later processing. then i would need to cast to long, and would need to take care to pass the right datatype to functions. probably its oldfashioned to treat pointers just as "long" values.&lt;BR /&gt;(alignchunky() makes sense for any memory address, it does not depend on the datatype. the system requires "even" alignment, and even "longword" alignment for some I/O data structures. in this case i need to declare the datatype, and to de-cast it each time i pass it to functions like alignchunky().)&lt;/DIV&gt;&lt;DIV&gt;one answer i found myself: not to mix various addressing methods/cast systems. even if the code size increases and execution slows down.&lt;/DIV&gt;&lt;DIV&gt;alex finn, dublin, ireland. electronic games and effective design.&lt;BR /&gt;information about superstition: &lt;A _jive_internal="true" href="https://community.nxp.com/external-link.jspa?url=http%3A%2F%2Fuk.geocities.com%2Fnikemoto2511" rel="nofollow" target="_blank"&gt;http://uk.geocities.com/nikemoto2511&lt;/A&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Jan 24, 2006, 1:42 AM&lt;/DIV&gt;&lt;DIV&gt;Post #4 of 4 (76 views)&lt;BR /&gt;Copy Shortcut&lt;BR /&gt;&amp;nbsp;Re: [ColdFire] c syntax: pointer type cast [In reply to]&amp;nbsp; Can't Post&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;--------------------------------------------------------------------------------&lt;BR /&gt;&amp;nbsp;&lt;BR /&gt;regards, however i have put in "long" for illustration purpose.&lt;BR /&gt;lthe way you state it: that's how it WAS.&lt;BR /&gt;but i can see now i labelled/commented it wrong:&lt;/DIV&gt;&lt;DIV&gt;&amp;gt;now i would like to compare two UBYTE values:&lt;BR /&gt;&amp;gt; if( (*(UBYTE *)src_data)==(*(UBYTE *)tags) ) {do_something();}&lt;/DIV&gt;&lt;DIV&gt;if it does not work correctly (inc/dec numeric variable), i need to add additional&lt;BR /&gt;casts (they do work, i have tested it). some documents explain that "pointer variables get increased by the datasize". however its difficult/interruptive for me to compile/test/debug it right now, hence i ask...&lt;/DIV&gt;&lt;DIV&gt;the background are arrays of pointers to structures which: contain pointers to others structures, memory areas and so on. it just does not make sense to make up highly specialized datatypes/pointer types, and then downcast them just to add a numerical value.&lt;/DIV&gt;&lt;DIV&gt;if an address (pointer=address representation) gets aligned to EVEN, 4byte,... it has nothing to do with the type of data, its just a numerical thing.&lt;/DIV&gt;&lt;DIV&gt;nikemoto2511&lt;/DIV&gt;&lt;DIV&gt;--------------------------------------------------------------------------------------------------------------&lt;/DIV&gt;&lt;DIV&gt;&lt;BR /&gt;Scott Hauck &amp;lt;&lt;A class="jive-link-email-small" href="mailto:haucks@nextnetwireless.com"&gt;haucks@nextnetwireless.com&lt;/A&gt;&amp;gt; wrote:&lt;BR /&gt;I see someone already replied to you, but I have just a couple more points.&lt;/DIV&gt;&lt;DIV&gt;&amp;gt; now i would like to compare two longwords:&lt;BR /&gt;&amp;gt; if( (*(UBYTE *)src_data)==(*(UBYTE *)tags) ) {do_something();}&lt;/DIV&gt;&lt;DIV&gt;src_data and tags are already of type long so if you wish&lt;BR /&gt;to compare longwords, then "(src_data == tags)" would do it&lt;BR /&gt;for you. The statement you have isn't just a byte compare,&lt;BR /&gt;but you are casting src_data and tags as pointers to bytes.&lt;BR /&gt;Therefore you statement is comparing the byte information&lt;BR /&gt;that is being pointed at by src_data and tags. Probably not&lt;BR /&gt;what you want...&lt;/DIV&gt;&lt;DIV&gt;&amp;gt; then i store a result:&lt;BR /&gt;&amp;gt; (*(ULONG *)dst_buffer)=data;&lt;/DIV&gt;&lt;DIV&gt;Another pointer problem here. dst_buffer is a long and you&lt;BR /&gt;have cast it to a pointer to an 'unsigned long'. So when you&lt;BR /&gt;store the data, you are actually storing it in a location&lt;BR /&gt;pointed to by dst_buffer.&lt;/DIV&gt;&lt;DIV&gt;&amp;gt; and then i go to the next longword data item:&lt;BR /&gt;&amp;gt; (ULONG *)dst_buffer++;&lt;/DIV&gt;&lt;DIV&gt;You don't need the cast. Just "dst_buffer++" is sufficient.&lt;/DIV&gt;&lt;DIV&gt;&amp;gt; or the datasize gets added:&lt;BR /&gt;&amp;gt; dst_buffer+=sizeof(struct Image); /* size of data structure */&lt;/DIV&gt;&lt;DIV&gt;The other responder was correct. When you increment a variable&lt;BR /&gt;it gets incremented by the size of its type. Likewise, when&lt;BR /&gt;adding to a variable, it gets whatever is being added *times*&lt;BR /&gt;the size of the receiving variable.&lt;/DIV&gt;&lt;DIV&gt;Pointers are a somewhat confusing element of C. In fact,&lt;BR /&gt;it is hardly even taught in school anymore. If you truly&lt;BR /&gt;want these things to be pointers, then declare them that way&lt;BR /&gt;instead of constantly having to cast them.&lt;/DIV&gt;&lt;DIV&gt;long *src_data;&lt;BR /&gt;long *tags;&lt;BR /&gt;long *dst_buffer;&lt;/DIV&gt;&lt;DIV&gt;# compare 2 longwords&lt;BR /&gt;if (*src_data == *dst_buffer) {do_something();}&lt;/DIV&gt;&lt;DIV&gt;# then store a result:&lt;BR /&gt;*dst_buffer = data;&lt;/DIV&gt;&lt;DIV&gt;# and then i go to the next longword data item:&lt;BR /&gt;dst_buffer++;&lt;/DIV&gt;&lt;DIV&gt;&lt;BR /&gt;-Scott&lt;/DIV&gt;&lt;DIV&gt;. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .&lt;/DIV&gt;&lt;DIV&gt;&lt;BR /&gt;&amp;nbsp;&lt;BR /&gt;&amp;nbsp;&lt;BR /&gt;&amp;nbsp;&lt;BR /&gt;&lt;/DIV&gt;&lt;P&gt;Message Edited by Dietrich on &lt;SPAN class="date_text"&gt;04-01-2006&lt;/SPAN&gt; &lt;SPAN class="time_text"&gt;11:34 AM&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;Message Edited by Dietrich on &lt;SPAN class="date_text"&gt;04-04-2006&lt;/SPAN&gt; &lt;SPAN class="time_text"&gt;09:33 PM&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 01 Apr 2006 06:30:24 GMT</pubDate>
      <guid>https://community.nxp.com/t5/ColdFire-68K-Microcontrollers/c-syntax-pointer-type-cast/m-p/130832#M772</guid>
      <dc:creator>Dietrich</dc:creator>
      <dc:date>2006-04-01T06:30:24Z</dc:date>
    </item>
  </channel>
</rss>

