delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2000/03/07/18:44:50

From: Hans-Bernhard Broeker <broeker AT acp3bf DOT physik DOT rwth-aachen DOT de>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Problems with extern and sizeof
Date: 7 Mar 2000 11:37:35 GMT
Organization: Aachen University of Technology (RWTH)
Lines: 68
Message-ID: <8a2plv$7u1$1@nets3.rz.RWTH-Aachen.DE>
References: <200003062236 DOT RAA14543 AT delorie DOT com>
NNTP-Posting-Host: acp3bf.physik.rwth-aachen.de
X-Trace: nets3.rz.RWTH-Aachen.DE 952429055 8129 137.226.32.75 (7 Mar 2000 11:37:35 GMT)
X-Complaints-To: abuse AT rwth-aachen DOT de
NNTP-Posting-Date: 7 Mar 2000 11:37:35 GMT
User-Agent: tin/1.4-19991113 ("No Labels") (UNIX) (Linux/2.0.0 (i586))
Originator: broeker@
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019