Mail Archives: djgpp/2000/03/07/18:44:50
Gorka <gorkau AT geocities DOT com> wrote:
> kk2.c: Error: sizeof applied to an incomplete type.
> Can anybody tell me why? And how to avoid it?
The source file that has the error is:
> /* file 2: kk2.c */
> extern char dias[];
> void pp(){
> int kk=sizeof dias; //poner este, o el siguiente (test)
> printf("\n***%s***",dias);
> printf("***kk= %d.\n",kk);
> }
[This whole thing is a basic C question, and not really DJGPP
specific, so it should have gone to the C newsgroup...]
It's quite simple. Nowhere in the source of 'kk2.c' is there any
information about how large the array 'dias[]' actually is. So how
could gcc be supposed to find out what to 'sizeof dias' should be? It
can't, thus the error message. You cannot use 'sizeof' for what
you're trying to do. You have two choices:
1) Change 'dias[]' into 'dias[4]', everywhere.
2) Store the size of 'dias' into a separate variable, in the main
sourcefile where this 'variable sized' array is actually initialized.
That's the only part of your program where gcc knows how large that
array is, currently. I.e. in mosqueo.c, you'll need to make the
following additions:
> /* file 1: mosqueo.c */
> void pp();
> char dias[]={'3','2','3',0};
size_t size_of_dias;
> int main(){
> int rr=sizeof dias;
size_of_dias = sizeof dias;
> system("clear"); /* sustituir por system("cls"); en msdos */
> pp();
> printf("\n---%s---",dias);
> printf("---r= %d.\n",rr);
> return(0);
> }
Then, in kk2.c, add an 'extern size_t size_of_dias;', and use that
instead of 'sizeof dias'. Thus you can export the knowledge about the
size of 'dias' via a global variable.
Proper software design would require to package these two, as in:
typedef struct variable_char_array {
size_t size;
char *content;
} variable_char_array;
variable_char_array dias = {4, {"323"}};
--
Hans-Bernhard Broeker (broeker AT physik DOT rwth-aachen DOT de)
Even if all the snow were burnt, ashes would remain.
- Raw text -