<?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: using sizeof() on externs in CodeWarrior for MCU</title>
    <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/using-sizeof-on-externs/m-p/148467#M3469</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;DIV&gt;Thanks, Guru.&amp;nbsp; Assigning another variable the value of the size and then using that variable later worked fine.&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Tue, 22 Apr 2008 20:17:45 GMT</pubDate>
    <dc:creator>irob</dc:creator>
    <dc:date>2008-04-22T20:17:45Z</dc:date>
    <item>
      <title>using sizeof() on externs</title>
      <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/using-sizeof-on-externs/m-p/148465#M3467</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hey guys, here's my setup:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;DIV&gt;&lt;DIV class="msg_source_code"&gt;&lt;DIV class="text_smallest"&gt;Code:&lt;/DIV&gt;&lt;PRE&gt;&lt;SPAN style="text-decoration: underline;"&gt;main.c&lt;/SPAN&gt;
#include "main.h"
const Byte array1[] = "sample string";

&lt;SPAN style="text-decoration: underline;"&gt;main.h&lt;/SPAN&gt;
#define ARRAY_SIZE sizeof(array1)

&lt;SPAN style="text-decoration: underline;"&gt;file2.c&lt;/SPAN&gt;
#include "main.h"
extern const Byte array1[];

for (i=0; i&amp;lt;ARRAY_SIZE; i++)
{
&amp;nbsp; Function(array1[i]);
}&lt;/PRE&gt;&lt;/DIV&gt;&lt;BR /&gt;What I'm getting with this is a C1838 error: Unknown object-size: sizeof (incomplete type).&lt;BR /&gt;&lt;BR /&gt;Yet, if I simply move that array declaration from main.c into file2.c where the array is used, all will compile.&amp;nbsp; The trouble is, I really need the array with the string to be located inside main.c.&lt;BR /&gt;&lt;BR /&gt;Any ideas what I'm doing wrong?&amp;nbsp; It's got to be with my extern, right?&lt;BR /&gt;&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 29 Oct 2020 08:50:15 GMT</pubDate>
      <guid>https://community.nxp.com/t5/CodeWarrior-for-MCU/using-sizeof-on-externs/m-p/148465#M3467</guid>
      <dc:creator>irob</dc:creator>
      <dc:date>2020-10-29T08:50:15Z</dc:date>
    </item>
    <item>
      <title>Re: using sizeof() on externs</title>
      <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/using-sizeof-on-externs/m-p/148466#M3468</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;That's how the C language is works, the compiler just does not know the size when only the declaration without size is known.&lt;BR /&gt;&lt;BR /&gt;As possible solutions you could:&lt;BR /&gt;- specify the array size in the declaration:&lt;BR /&gt;&lt;PRE&gt;&lt;U&gt;&lt;U&gt;main.h:&lt;/U&gt;&lt;/U&gt;

extern const Byte array1[14];

The disadvantage is obviously that you have to maintain the array
size explicitly. The compiler will check the size at least when he
sees the declaration and the definition while compiling main.c
(say when the extern declaration is in main.h)

-define another constant with the size.
&lt;U&gt;&lt;U&gt;main.h:&lt;/U&gt;&lt;/U&gt;
extern const Byte array1[];
extern const size_t array1_size;
#define ARRAY_SIZE array1_size
main.c:
#include "main.h"
const Byte array1[] = "Sample";
const size_t array1_size = sizeof(array1);

The disadvantage here is that the code is a little bit less efficient
as the array size is no longer known at compile time, but has to be
actually read from a constant at runtime instead.

Daniel


&lt;/PRE&gt;&lt;BR /&gt;&lt;DIV&gt;&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 29 Oct 2020 08:50:17 GMT</pubDate>
      <guid>https://community.nxp.com/t5/CodeWarrior-for-MCU/using-sizeof-on-externs/m-p/148466#M3468</guid>
      <dc:creator>CompilerGuru</dc:creator>
      <dc:date>2020-10-29T08:50:17Z</dc:date>
    </item>
    <item>
      <title>Re: using sizeof() on externs</title>
      <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/using-sizeof-on-externs/m-p/148467#M3469</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;DIV&gt;Thanks, Guru.&amp;nbsp; Assigning another variable the value of the size and then using that variable later worked fine.&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 22 Apr 2008 20:17:45 GMT</pubDate>
      <guid>https://community.nxp.com/t5/CodeWarrior-for-MCU/using-sizeof-on-externs/m-p/148467#M3469</guid>
      <dc:creator>irob</dc:creator>
      <dc:date>2008-04-22T20:17:45Z</dc:date>
    </item>
    <item>
      <title>Re: using sizeof() on externs</title>
      <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/using-sizeof-on-externs/m-p/148468#M3470</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;To simply hardcode the array size seems to be the best solution to me. There is no such thing as "dynamic array length" in an embedded system, and there shouldn't be either. There is always a maximum length: the number of symbols available on the LCD etc.&lt;BR /&gt;&lt;BR /&gt;Though, in this particular case, this would be another acceptable solution:&lt;BR /&gt;&lt;BR /&gt;unsigned char a_size = strlen(array1);&lt;BR /&gt;&lt;BR /&gt;for (i=0; i a_size; i++)&lt;BR /&gt;{&lt;BR /&gt;Function(array1[i]);&lt;BR /&gt;}&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 22 Apr 2008 20:23:51 GMT</pubDate>
      <guid>https://community.nxp.com/t5/CodeWarrior-for-MCU/using-sizeof-on-externs/m-p/148468#M3470</guid>
      <dc:creator>Lundin</dc:creator>
      <dc:date>2008-04-22T20:23:51Z</dc:date>
    </item>
    <item>
      <title>Re: using sizeof() on externs</title>
      <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/using-sizeof-on-externs/m-p/148469#M3471</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;DIV&gt;Lundin, you're absolutely right.&amp;nbsp; In this case, my string is for storing the firmware version number.&amp;nbsp; For me, this can be from 3 characters&amp;nbsp;("1.0") up to 5 ("1.2.3").&amp;nbsp; Since that varies for me, I'd rather leave open the option to put as many characters I need.&amp;nbsp; And since I'm the sole maintainer of this firmware, I can make sure that it's in an available flash range.&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 22 Apr 2008 22:38:35 GMT</pubDate>
      <guid>https://community.nxp.com/t5/CodeWarrior-for-MCU/using-sizeof-on-externs/m-p/148469#M3471</guid>
      <dc:creator>irob</dc:creator>
      <dc:date>2008-04-22T22:38:35Z</dc:date>
    </item>
  </channel>
</rss>

