From: al_bin <al...@vp...> - 2013-06-10 08:15:24
|
W dniu 2013-06-10 00:53:12 użytkownik Antonio Todo Bom <an...@bo...> napisał: YES ! But almost that: const char * const menu_str[][] = ... const char * const char menu_str[5][2] =... char identifier was missing, and we need to declare the number of itens or will note compile. And Henry, My target device is a PIC18F24K22 Thank you Henry and Marten. :-) :-) What is role of second char in your declaration? Try: const char * const float menu_str[5][2] =... And as Marteen sugest: const char * const menu_str[][2] =... Yes, number in second [] is necessery. Why? Albert |
From: al_bin <al...@vp...> - 2013-06-10 09:51:01
|
W dniu 2013-06-10 10:46:56 użytkownik Maarten Brock <sou...@ds...> napisał: > If SDCC requires the second char then that is a bug. Second char isn't required. > Even if SDCC accepts the second char it is a bug. That is I point out, less explicit than you ;-) > > The C standard requires that only the first level of a multi-dimensional > array can have unspecified size. It is not allowed to create an array with > incomplete type elements. Don't ask me why. I ask Antonio, not you. > But I thought that SDCC would > accept multiple unspecified sizes in --std-sdccXX mode with a fully braced > and specified initializer as used here. I don't think so. Regards, Albert |
From: Maarten B. <sou...@ds...> - 2013-06-10 08:47:05
|
> W dniu 2013-06-10 00:53:12 użytkownik Antonio Todo Bom > <an...@bo...> napisaÅ: > YES ! > But almost that: > > const char * const menu_str[][] = ... > > const char * const char menu_str[5][2] =... > > char identifier was missing, and we need to declare the number of itens or > will note compile. > > And Henry, > My target device is a PIC18F24K22 > > Thank you Henry and Marten. > > :-) :-) > > What is role of second char in your declaration? > > Try: > const char * const float menu_str[5][2] =... > > And as Marteen sugest: > const char * const menu_str[][2] =... > > Yes, number in second [] is necessery. Why? > > Albert If SDCC requires the second char then that is a bug. Even if SDCC accepts the second char it is a bug. The C standard requires that only the first level of a multi-dimensional array can have unspecified size. It is not allowed to create an array with incomplete type elements. Don't ask me why. But I thought that SDCC would accept multiple unspecified sizes in --std-sdccXX mode with a fully braced and specified initializer as used here. Maarten |
From: Sebastien L. <seb...@lo...> - 2013-06-10 12:15:55
|
Le 10/06/2013 10:15, al_bin a écrit : > Yes, number in second [] is necessery. Why? Here is what I think, which may or may not be correct: Because in C a 2-dimensional array is defined as an array of arrays. Which is the same as a pointer to array. And you can't have pointer to undefined types, so only the first dimension is optional. A quick test with gcc for the following code: int a[][] = {{1,1},{2,2},{3,3} }; tells: test.c:1: error: array type has incomplete element type so that seems to confirm my suggestion with one precision: you can't declare arrays when the *element type* is unknown. --- A compiler extension could count the number of initializer entries and use that, but there is no guarantee that the number of initializers will always match the size of the initialized array. How is the compiler supposed to deal with that : int a[][] = {{1},{2,2},{3,3,3} }; If we try to think as a compiler: Okay, this array has 3 "main" elements, but what is the element size? you need the type to compute that. Is it int[3] where all elements are not initialized? or is it int[2] with too many initializers in the third entry? is it something else? This information is required to allocate memory for array elements! >From the low-level perspective of the C language, it feels safe to prevent such automatic and ambiguous data definitions. BR Sebastien PS: On the contrary, you can have pointers to undefined structures, in declarations such as : struct node { struct node *next; int value; }; you can even have undefined structure *pointers* where the structure is defined elsewhere: struct undefined_struct *global_variable; This will compile, and you can use this global variable as a pointer, but as soon as you try to dereference the pointer somewhere (with global_variable->member), you'll get an undefined type error, because the compiler does not know which members are available. |