<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://community.arm.com/utility/feedstylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>The &amp;quot;sizeof operator with string&amp;quot; bug when use command line in &amp;quot;output window&amp;quot;</title><link>https://community.arm.com/developer/tools-software/tools/f/keil-forum/23243/the-sizeof-operator-with-string-bug-when-use-command-line-in-output-window</link><description> 
 I do not speak english,so i try to show my meaning 

 
When you use command line in debug mode, 
sizeof(&amp;quot;a&amp;quot;) //value is 1 
sizeof(&amp;#39;a&amp;#39;) //value is 2 
would got wrong value. 

 
seems been upended. 

 
IDE-Version: 
&amp;#231;&amp;#164;&amp;#166;ision3 V3.53 
Copyright (c) Keil</description><dc:language>en-US</dc:language><generator>Telligent Community 10</generator><item><title>RE: The "sizeof operator with string" bug when use command line in "output window"</title><link>https://community.arm.com/thread/100174?ContentTypeID=1</link><pubDate>Sun, 20 Jan 2008 17:25:35 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:7bd363c3-da33-42ef-996e-5ba2648f8520</guid><dc:creator>Bob Zhu</dc:creator><description>&lt;p&gt;&lt;p&gt;
What I want to say is &amp;quot;sizeof operator with string or char in
command line (which in the uvision3&amp;#39;s debug mode)returns wrong
value&amp;quot;&lt;br /&gt;
you can try it in uvision3.&lt;/p&gt;

&lt;pre&gt;
int main(void)
{
    printf(&amp;quot;sizeof(\&amp;quot;1234\&amp;quot;) = %u\n&amp;quot;, (unsigned)sizeof(&amp;quot;1234&amp;quot;));
}
&lt;/pre&gt;

&lt;p&gt;
&lt;br /&gt;
bulid an exe file,The program works fine.&lt;br /&gt;
BUT IN DEBUG mode ( when you press &amp;quot;start debug session&amp;quot; buttom,click
the &amp;quot;command&amp;quot; option which in the &amp;quot;outputwindow&amp;quot; and key in such as
&amp;quot;sizeof(&amp;quot;1234&amp;quot;)&amp;quot; )it would return 4&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: The "sizeof operator with string" bug when use command line in "output window"</title><link>https://community.arm.com/thread/113584?ContentTypeID=1</link><pubDate>Sat, 19 Jan 2008 17:30:18 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:9758374c-4a8f-4254-b9d7-0510e5752301</guid><dc:creator>Dan Henry</dc:creator><description>&lt;p&gt;&lt;p&gt;
&lt;i&gt;&amp;quot;So a string literal has type &amp;quot;array of char&amp;quot; and not &amp;quot;pointer
to char&amp;quot;?&amp;quot;&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;
Right. I sometimes use sizeof with a string literal argument in
the following context to size a buffer not too big, but just right,
along with providing some bit of documentation as to how something
will end up being formatted.&lt;/p&gt;

&lt;pre&gt;
char buf[sizeof "Temp: -ttt  Hum: hh%"];

sprintf(buf, &amp;quot;Temp: %4d  Hum: %2u%%&amp;quot;, (int)temperature, (unsigned)humidity);
&lt;/pre&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: The "sizeof operator with string" bug when use command line in "output window"</title><link>https://community.arm.com/thread/100156?ContentTypeID=1</link><pubDate>Sat, 19 Jan 2008 16:44:03 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:cdc544c8-2bd2-468d-9955-97e692178901</guid><dc:creator>Drew Davis</dc:creator><description>&lt;p&gt;&lt;p&gt;
So a string literal has type &amp;quot;array of char&amp;quot; and not &amp;quot;pointer to
char&amp;quot;? Thanks for the correction.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: The "sizeof operator with string" bug when use command line in "output window"</title><link>https://community.arm.com/thread/88822?ContentTypeID=1</link><pubDate>Sat, 19 Jan 2008 15:28:47 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:e979580f-2583-436a-9fa7-bfb8d1c2a949</guid><dc:creator>Dan Henry</dc:creator><description>&lt;p&gt;&lt;p&gt;
&lt;i&gt;&amp;quot;sizeof(&amp;quot;a&amp;quot;) returns the size of a pointer to char.&amp;quot;&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;
From C99 6.3.2.1 par. 3:&lt;/p&gt;

&lt;p&gt;
&lt;i&gt;&amp;quot;Except when it is the operand of the sizeof operator or the
unary &amp;amp; operator, or is a string literal used to initialize an
array, an expression that has type &amp;quot;array of type&amp;quot; is converted to an
expression with type &amp;quot;pointer to type&amp;quot; that points to the initial
element of the array object and is not an lvalue.&amp;quot;&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;
For example:&lt;/p&gt;

&lt;pre&gt;
t.c:
#include &amp;lt;stdio.h&amp;gt;

int main(void)
{
    printf(&amp;quot;sizeof(\&amp;quot;1234\&amp;quot;) = %u\n&amp;quot;, (unsigned)sizeof(&amp;quot;1234&amp;quot;));
    printf(&amp;quot;sizeof \&amp;quot;1234\&amp;quot;  = %u\n&amp;quot;, (unsigned)sizeof &amp;quot;1234&amp;quot; );
    return 0;
}

$ gcc -Wall -ansi -pedantic t.c

$ ./a
sizeof(&amp;quot;1234&amp;quot;) = 5
sizeof &amp;quot;1234&amp;quot;  = 5
&lt;/pre&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: The "sizeof operator with string" bug when use command line in "output window"</title><link>https://community.arm.com/thread/51425?ContentTypeID=1</link><pubDate>Sat, 19 Jan 2008 14:34:35 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:ca00b392-9499-4f25-ba1d-79f1cbb4dd7f</guid><dc:creator>Drew Davis</dc:creator><description>&lt;p&gt;&lt;p&gt;
There&amp;#39;s no such thing as a string data type in C. The standard
library supports a type, which happens to be built on the types
&amp;quot;char&amp;quot; and &amp;quot;pointer to char&amp;quot;. There&amp;#39;s also a &amp;quot;string literal&amp;quot; useful
for initializing arrays of char. But &amp;quot;string&amp;quot; is not really a
first-class data type in the language.&lt;/p&gt;

&lt;p&gt;
sizeof returns the size (in units of the size of a char, which is
not necessarily one byte, though that&amp;#39;s extremely common) of the type
of the expression to which it is applied. Note that it&amp;#39;s technically
an operator, not a function, even though almost everyone writes their
code as though it were a function. The parens are there from habit,
but also so you don&amp;#39;t have to worry about the precedence of sizeof
relative to the other operators in your intended expression.&lt;/p&gt;

&lt;p&gt;
sizeof(&amp;quot;a&amp;quot;) returns the size of a pointer to char. Depending on
the memory model, this might actually be one byte long. A
pointer-to-data or pointer-to-pdata is one byte. Pointers to xdata
are two. &amp;quot;Generic&amp;quot; pointers (to unknown memory type, so they&amp;#39;re
type-tagged) or pointers to far memory are three bytes long.&lt;/p&gt;

&lt;p&gt;
sizeof(&amp;#39;a&amp;#39;) is the size of the type of &amp;#39;a&amp;#39;. &amp;#39;a&amp;#39; is more formally
known as an &amp;quot;integer character constant&amp;quot;, and -- oddly enough -- has
type &amp;quot;int&amp;quot; and not type &amp;quot;char&amp;quot;. So, sizeof(&amp;#39;a&amp;#39;) is the same as
sizeof(int), which is 2 for Keil C51.&lt;/p&gt;

&lt;p&gt;
If you want to count the number of characters in a string, use the
library function strlen().&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>