#2059 Compiler error with string initializer constants

closed-fixed
Ben Shi
Front-end
5
2015-05-24
2012-07-04
Anonymous
No

Found this today...

The following code emits a strange error message

void foo(void)
{
static char x[] = {"Hello, World!\n"};
}

Compiler emits error as follows:

-:0: error 47: indirections to different types assignment
from type 'const-char [15d] code'
to type 'char auto'

It appears that an invalid line number is generated.
Also, this code should not be a problem. gcc has no problem with it.
I simply want to initialize a character array using a string constant contained within curly braces.

I downloaded the most recent SDCC tarball from SorceForge this evening and built it on my Ubuntu 12.04 64-bit system.
Here is the compiler version string:
SDCC : mcs51/gbz80/z80/z180/r2k/r3ka/ds390/pic16/pic14/TININative/ds400/hc08/s08 3.2.0 #7999 (Jul 4 2012) (Linux)

-Pete
peter UNDERSCORE arnt AT yahoo DOT com

Discussion

  • Raphael Neider

    Raphael Neider - 2012-07-04

    {"Hello"} is a valid initializer for a struct, containing a char* as its first element AND for an array of char* (that is pointers to char). In my opinion, it is not a valid initializer for an array of chars.
    Just leave out the curly braces and you are good to go.
    The other strangenesses reported (filename/line numbering) remain valid ...

     
  • Philipp Klaus Krause

    "An array of character type may be initialized by a character string literal or UTF−8 string
    literal, optionally enclosed in braces. Successive bytes of the string literal (including the
    terminating null character if there is room or if the array is of unknown size) initialize the
    elements of the array." - page 140, §6.7.9, N1570, comitee draft, April 12, 2011, aka the C11 standard.

    AFAIK it is the same in earlier standards.

    Philipp

     
  • Maarten Brock

    Maarten Brock - 2012-07-06

    This falls in the same category as multidimensional arrays which need to be fully braced for SDCC where the standard allows single braces.

    char array1 [2][2] = {1, 2, 3, 4}; //allowed in C99
    char array2 [2][2] = {{1, 2}, {3, 4}}; //necessary braces for SDCC

    And don't ask me why the standard is so ambiguous. IMHO it would have been better if they allowed this:
    char array3 [ ][ ] = {{1}, {2,3}}; //should automatically create 2x2 array

    I cannot reproduce the missing filename and I have a fix for the missing linenumber and accidental d after 15 which I will apply after the release.

     
  • Philipp Klaus Krause

    While I agree with you, maarten, the rationale for allowing the weirder forms is that it is supposed to make automatic generaton of C code easier.

    Philipp

     
  • Maarten Brock

    Maarten Brock - 2012-07-15

    Missing linenumber and accidental 'd' fixed in SDCC 3.2.1 #8044.

     
  • wek

    wek - 2014-01-04

    The line number points confusingly to the end of file, rather than to the line where the initialization happens.

    Jan Waclawek

    c:\wek\tmp>c:\progra~1\sdcc.latest\bin\sdcc.exe -v
    SDCC : mcs51/z80/z180/r2k/r3ka/gbz80/tlcs90/ds390/pic16/pic14/TININative/ds400/h
    c08/s08/stm8 3.3.2 #8935 (Jan  3 2014) (MINGW64)
    published under GNU General Public License (GPL)
    
    c:\wek\tmp>c:\progra~1\sdcc.latest\bin\sdcc.exe a.c
    a.c:5: error 47: indirections to different types assignment
    from type 'const-char [4] code'
      to type 'char fixed'
    
    c:\wek\tmp>type a.c
    char a[] = {"ABC"};
    
    void main(void) {
    }
    
    c:\wek\tmp>
    
     
  • Ben Shi

    Ben Shi - 2015-03-09
    • Category: --> Front-end
     
  • Ben Shi

    Ben Shi - 2015-05-23
    • status: open --> closed-fixed
    • assigned_to: Ben Shi
     
  • Ben Shi

    Ben Shi - 2015-05-23

    Fixed in reversion #9240.

     
  • Ben Shi

    Ben Shi - 2015-05-23
    • status: closed-fixed --> open
     
  • Ben Shi

    Ben Shi - 2015-05-23

    Sorry, the char *p = {"abc"} form initialization is supported.

    But char arr[][] = {1, 2, 3, 4} is still not supported.

     
  • Ben Shi

    Ben Shi - 2015-05-23

    I tried gcc 4.9 with
    int q[][] = {{1, 2, 3}, {2,3,3}};

    and it gave error as
    a.c:1:5: error: array type has incomplete element type
    int q[][] = {{1, 2, 3}, {2,3,3}};
    ^

     
  • Ben Shi

    Ben Shi - 2015-05-24
    • status: open --> closed-fixed
     

Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks