<?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 Re: pointer array subtraction problem in CodeWarrior for MCU</title>
    <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/pointer-array-subtraction-problem/m-p/195780#M7225</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;Thanks for that.&amp;nbsp; I was aware that it wasn't &lt;EM&gt;really&lt;/EM&gt; the right way of doing things, but thought it would be considerabbly more efficient than a &lt;FONT face="courier new,courier"&gt;strlen()&lt;/FONT&gt; call.&amp;nbsp; I am surprised that the compiler didn't give me a warning, though (especially given some of the things it &lt;EM&gt;does&lt;/EM&gt; warn me about).&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 02 Jul 2009 16:57:01 GMT</pubDate>
    <dc:creator>Pyriform</dc:creator>
    <dc:date>2009-07-02T16:57:01Z</dc:date>
    <item>
      <title>pointer array subtraction problem</title>
      <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/pointer-array-subtraction-problem/m-p/195778#M7223</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am using CodeWarrior with a MC9S08QE128 and have found a curious probelm when trying to subtract elements of arrays when they are pointers.&amp;nbsp; Here is a sample of code which reproduces the problem:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;
const char *const ordinals[]={&amp;nbsp; "first", "second", "third"};void main(void) {&amp;nbsp; const char *o1p;&amp;nbsp; const char *o2p;&amp;nbsp; int o1len;&amp;nbsp; // I would expect this to assign to o1len the length of the string pointed&amp;nbsp; // to by ordinals[0]&amp;nbsp; o1len=ordinals[1]-ordinals[0];&amp;nbsp;&amp;nbsp;&amp;nbsp; // This really does assign to o1len the length of the string pointed to by&amp;nbsp; // ordinals[0]&amp;nbsp; o1p=ordinals[0];&amp;nbsp; o2p=ordinals[1];&amp;nbsp; o1len=o2p-o1p;

...

}&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; The first subtraction actually assignes a value of 2 to o1len, presumably because it is subtracting the addresses of the elements, rather than their actual pointer values.&amp;nbsp; Here is the listing produced:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;
ANSI-C/cC++ Compiler for HC08 V-5.0.24 Build 7134, May 15 2007&amp;nbsp;&amp;nbsp;&amp;nbsp; 1:&amp;nbsp; #include &amp;lt;hidef.h&amp;gt; /* for EnableInterrupts macro */&amp;nbsp;&amp;nbsp;&amp;nbsp; 2:&amp;nbsp; #include "derivative.h" /* include peripheral declarations */&amp;nbsp;&amp;nbsp;&amp;nbsp; 3:&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 4:&amp;nbsp; const char *const ordinals[]=&amp;nbsp;&amp;nbsp;&amp;nbsp; 5:&amp;nbsp; {&amp;nbsp;&amp;nbsp;&amp;nbsp; 6:&amp;nbsp;&amp;nbsp;&amp;nbsp; "first", "second", "third"&amp;nbsp;&amp;nbsp;&amp;nbsp; 7:&amp;nbsp; };&amp;nbsp;&amp;nbsp;&amp;nbsp; 8:&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 9:&amp;nbsp; void main(void)&amp;nbsp;&amp;nbsp;&amp;nbsp; 10:&amp;nbsp; {Function: main

...

&amp;nbsp; 0000 a7fa&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; [2]&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; AIS&amp;nbsp;&amp;nbsp; #-6&amp;nbsp;&amp;nbsp; 11:&amp;nbsp;&amp;nbsp;&amp;nbsp; const char *o1p;&amp;nbsp;&amp;nbsp; 12:&amp;nbsp;&amp;nbsp;&amp;nbsp; const char *o2p;&amp;nbsp;&amp;nbsp; 13:&amp;nbsp;&amp;nbsp;&amp;nbsp; int o1len;&amp;nbsp;&amp;nbsp; 14:&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 15:&amp;nbsp;&amp;nbsp;&amp;nbsp; // I would expect this to assign to o1len the length of the string pointed&amp;nbsp;&amp;nbsp; 16:&amp;nbsp;&amp;nbsp;&amp;nbsp; // to by ordinals[0]&amp;nbsp;&amp;nbsp; 17:&amp;nbsp;&amp;nbsp;&amp;nbsp; o1len=ordinals[1]-ordinals[0];&amp;nbsp; 0002 ae02&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; [2]&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; LDX&amp;nbsp;&amp;nbsp; #2&amp;nbsp; 0004 8c&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; [1]&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; CLRH&amp;nbsp;&amp;nbsp;&amp;nbsp; 0005 9eff05&amp;nbsp;&amp;nbsp; [5]&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; STHX&amp;nbsp; 5,SP&amp;nbsp;&amp;nbsp; 18:&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 19:&amp;nbsp;&amp;nbsp;&amp;nbsp; // This really does assign to o1len the length of the string pointed to by&amp;nbsp;&amp;nbsp; 20:&amp;nbsp;&amp;nbsp;&amp;nbsp; // ordinals[0]&amp;nbsp;&amp;nbsp; 21:&amp;nbsp;&amp;nbsp;&amp;nbsp; o1p=ordinals[0];&amp;nbsp; 0008 320000&amp;nbsp;&amp;nbsp; [5]&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; LDHX&amp;nbsp; ordinals&amp;nbsp; 000b 9eff01&amp;nbsp;&amp;nbsp; [5]&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; STHX&amp;nbsp; 1,SP&amp;nbsp;&amp;nbsp; 22:&amp;nbsp;&amp;nbsp;&amp;nbsp; o2p=ordinals[1];&amp;nbsp; 000e 320002&amp;nbsp;&amp;nbsp; [5]&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; LDHX&amp;nbsp; ordinals:2&amp;nbsp; 0011 9eff03&amp;nbsp;&amp;nbsp; [5]&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; STHX&amp;nbsp; 3,SP&amp;nbsp;&amp;nbsp; 23:&amp;nbsp;&amp;nbsp;&amp;nbsp; o1len=o2p-o1p;&amp;nbsp; 0014 95&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; [2]&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; TSX&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0015 e603&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; [3]&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; LDA&amp;nbsp;&amp;nbsp; 3,X&amp;nbsp; 0017 e001&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; [3]&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; SUB&amp;nbsp;&amp;nbsp; 1,X&amp;nbsp; 0019 87&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; [2]&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; PSHA&amp;nbsp;&amp;nbsp;&amp;nbsp; 001a e602&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; [3]&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; LDA&amp;nbsp;&amp;nbsp; 2,X&amp;nbsp; 001c f2&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; [3]&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; SBC&amp;nbsp;&amp;nbsp; ,X&amp;nbsp; 001d 87&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; [2]&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; PSHA&amp;nbsp;&amp;nbsp;&amp;nbsp; 001e 8a&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; [3]&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; PULH&amp;nbsp;&amp;nbsp;&amp;nbsp; 001f 88&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; [3]&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; PULX&amp;nbsp;&amp;nbsp;&amp;nbsp; 0020 9eff05&amp;nbsp;&amp;nbsp; [5]&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; STHX&amp;nbsp; 5,SP&amp;nbsp;&amp;nbsp; 24:&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 25: &lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;The second subtract does what I expected, so I have a solution to this problem, but I do not understand what is wrong with the first case.&amp;nbsp; Am I doing something silly, or is it a compiler bug?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;David.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 29 Oct 2020 09:50:57 GMT</pubDate>
      <guid>https://community.nxp.com/t5/CodeWarrior-for-MCU/pointer-array-subtraction-problem/m-p/195778#M7223</guid>
      <dc:creator>Pyriform</dc:creator>
      <dc:date>2020-10-29T09:50:57Z</dc:date>
    </item>
    <item>
      <title>Re: pointer array subtraction problem</title>
      <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/pointer-array-subtraction-problem/m-p/195779#M7224</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Dear Pyriform,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;As you suggest, this does look like a compiler bug to me - or at least an unintentional operation.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Using&lt;/P&gt;&lt;PRE&gt;o1len=(int)ordinals[1]-(int)ordinals[0];
&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;produces the expected value.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However I believe that pointer arithmetic is only guaranteed to be sensible for pointers into the same object (and one element beyond for arrays).&amp;nbsp; So perhaps this isn't really a bug since the operation you are doing is, strictly speaking, undefined in C.&amp;nbsp; In practice I would also expect it to work as you expected.&amp;nbsp; It is certainly not portable code.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;bye&lt;/P&gt;&lt;DIV class="message-edit-history"&gt;&lt;SPAN class="edit-author"&gt;Message Edited by pgo on&lt;/SPAN&gt; &lt;SPAN class="local-date"&gt;2009-07-02&lt;/SPAN&gt; &lt;SPAN class="local-time"&gt;10:47 AM&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 29 Oct 2020 09:50:59 GMT</pubDate>
      <guid>https://community.nxp.com/t5/CodeWarrior-for-MCU/pointer-array-subtraction-problem/m-p/195779#M7224</guid>
      <dc:creator>pgo</dc:creator>
      <dc:date>2020-10-29T09:50:59Z</dc:date>
    </item>
    <item>
      <title>Re: pointer array subtraction problem</title>
      <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/pointer-array-subtraction-problem/m-p/195780#M7225</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;Thanks for that.&amp;nbsp; I was aware that it wasn't &lt;EM&gt;really&lt;/EM&gt; the right way of doing things, but thought it would be considerabbly more efficient than a &lt;FONT face="courier new,courier"&gt;strlen()&lt;/FONT&gt; call.&amp;nbsp; I am surprised that the compiler didn't give me a warning, though (especially given some of the things it &lt;EM&gt;does&lt;/EM&gt; warn me about).&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 02 Jul 2009 16:57:01 GMT</pubDate>
      <guid>https://community.nxp.com/t5/CodeWarrior-for-MCU/pointer-array-subtraction-problem/m-p/195780#M7225</guid>
      <dc:creator>Pyriform</dc:creator>
      <dc:date>2009-07-02T16:57:01Z</dc:date>
    </item>
    <item>
      <title>Re: pointer array subtraction problem</title>
      <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/pointer-array-subtraction-problem/m-p/195781#M7226</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;First of all, look at the declaration. You have declared an array of constant pointers to various constant strings. You have also declared 3 independent constant strings (arrays). You have not declared an array of strings.&lt;BR /&gt;&lt;BR /&gt;Be aware of that you have allocated 3*sizeof(const char* const) + sizeof("first") + sizeof("second") + sizeof("third") bytes.&lt;BR /&gt;&lt;BR /&gt;So the three pointers point at 3 different arrays. These arrays may be allocated at entirely different places in the constant memory, as they are declared independantly, and not as a 2D array.&lt;BR /&gt;&lt;BR /&gt;As stated earlier, you cannot do pointer subtractions on pointers pointing at different arrays: this is undefined behavior in C, and it is the responsibility of the programmer to check for this.&lt;BR /&gt;&lt;BR /&gt;Had you declared a 2D array, the code would work as you expected (though this might be less effective memory-wise):&lt;BR /&gt;&lt;BR /&gt;const char ordinals[][7]=&lt;BR /&gt;{&lt;BR /&gt;"first", "second", "third"&lt;BR /&gt;};&lt;BR /&gt;&lt;BR /&gt;o1len=ordinals[1]-ordinals[0]; /* will give result 7 */&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;I suspect your problems stem from confusion about the difference of pointers and arrays in C. Here is some good reading on the subject: &lt;A href="http://c-faq.com/aryptr/index.html" target="test_blank"&gt;http://c-faq.com/aryptr/index.html&lt;/A&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 07 Jul 2009 21:28:20 GMT</pubDate>
      <guid>https://community.nxp.com/t5/CodeWarrior-for-MCU/pointer-array-subtraction-problem/m-p/195781#M7226</guid>
      <dc:creator>Lundin</dc:creator>
      <dc:date>2009-07-07T21:28:20Z</dc:date>
    </item>
    <item>
      <title>Re: pointer array subtraction problem</title>
      <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/pointer-array-subtraction-problem/m-p/195782#M7227</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I filed a (internal) bugreport on the initial code snippet. The code is indeed "undefined behavior", but the code the compiler generates is not ok regardless.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Daniel&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 07 Jul 2009 22:12:27 GMT</pubDate>
      <guid>https://community.nxp.com/t5/CodeWarrior-for-MCU/pointer-array-subtraction-problem/m-p/195782#M7227</guid>
      <dc:creator>CompilerGuru</dc:creator>
      <dc:date>2009-07-07T22:12:27Z</dc:date>
    </item>
  </channel>
</rss>

