This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

The "sizeof operator with string" bug when use command line in "output window"

I do not speak english,so i try to show my meaning

When you use command line in debug mode,
sizeof("a") //value is 1
sizeof('a') //value is 2
would got wrong value.

seems been upended.

IDE-Version:
礦ision3 V3.53
Copyright (c) Keil Elektronik GmbH / Keil Software, Inc. 1995 - 2007
Tool Version Numbers:
Toolchain Path: C:\KeilC51\C51\BIN\
C Compiler: C51.Exe V8.09
Assembler: A51.Exe V8.00d
Linker/Locator: BL51.Exe V6.05
Librarian: LIB51.Exe V4.24
Hex Converter: OH51.Exe V2.6
CPU DLL: S8051.DLL V3.12
Dialog DLL: DCYG.DLL V2.44d

Parents
  • "sizeof("a") returns the size of a pointer to char."

    From C99 6.3.2.1 par. 3:

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

    For example:

    t.c:
    #include <stdio.h>
    
    int main(void)
    {
        printf("sizeof(\"1234\") = %u\n", (unsigned)sizeof("1234"));
        printf("sizeof \"1234\"  = %u\n", (unsigned)sizeof "1234" );
        return 0;
    }
    
    $ gcc -Wall -ansi -pedantic t.c
    
    $ ./a
    sizeof("1234") = 5
    sizeof "1234"  = 5
    

Reply
  • "sizeof("a") returns the size of a pointer to char."

    From C99 6.3.2.1 par. 3:

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

    For example:

    t.c:
    #include <stdio.h>
    
    int main(void)
    {
        printf("sizeof(\"1234\") = %u\n", (unsigned)sizeof("1234"));
        printf("sizeof \"1234\"  = %u\n", (unsigned)sizeof "1234" );
        return 0;
    }
    
    $ gcc -Wall -ansi -pedantic t.c
    
    $ ./a
    sizeof("1234") = 5
    sizeof "1234"  = 5
    

Children
  • So a string literal has type "array of char" and not "pointer to char"? Thanks for the correction.

  • "So a string literal has type "array of char" and not "pointer to char"?"

    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.

    char buf[sizeof "Temp: -ttt  Hum: hh%"];
    
    sprintf(buf, "Temp: %4d  Hum: %2u%%", (int)temperature, (unsigned)humidity);
    

  • What I want to say is "sizeof operator with string or char in command line (which in the uvision3's debug mode)returns wrong value"
    you can try it in uvision3.

    int main(void)
    {
        printf("sizeof(\"1234\") = %u\n", (unsigned)sizeof("1234"));
    }
    


    bulid an exe file,The program works fine.
    BUT IN DEBUG mode ( when you press "start debug session" buttom,click the "command" option which in the "outputwindow" and key in such as "sizeof("1234")" )it would return 4