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
{"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 ...
"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
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.
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
Missing linenumber and accidental 'd' fixed in SDCC 3.2.1 #8044.
The line number points confusingly to the end of file, rather than to the line where the initialization happens.
Jan Waclawek
Fixed in reversion #9240.
Sorry, the char *p = {"abc"} form initialization is supported.
But char arr[][] = {1, 2, 3, 4} is still not supported.
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}};
^