www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/01/02/18:13:16

From: Erik Max Francis <max AT alcyone DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Help needed with sizeof command.
Date: Sat, 02 Jan 1999 15:04:01 -0800
Organization: Alcyone Systems
Lines: 73
Message-ID: <368EA5E1.63E4B1C8@alcyone.com>
References: <368E9EE0 DOT CA5E941B AT northcoast DOT com>
NNTP-Posting-Host: charmaine.alcyone.com
Mime-Version: 1.0
X-Mailer: Mozilla 4.5 [en] (X11; I; Linux 2.0.34 i686)
X-Accept-Language: en, eo
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

David Arnold wrote:

> I am new to djgpp and C, so be kind. I am working through a book on C
> and the program
> 
> #include <stdio.h>
> #include <conio.h>
> void clrscr(void);
> int days[]={31,28,31,30,31,30,31,31,30,31};
> main()
> {
>  int index;
>  extern int days[];
> 
>  clrscr();
> 
>  for(index=0;index < sizeof days/(sizeof(int));index++)
>     printf("Month %d has %d days.\n",index+1,days[index]);
>  return 0;
> }
> 
> bombs with error message "sizeof applied to incomplete type." I tried
> Ctrl+F1, but no help provided. Apparently, the sizeof command is not
> part of libc? Am I correct in this?

sizeof is an internal operator in C, and is always useable.

This works fine for me under gcc 2.7.2.2 (Linux).  I suspect a problem
may be the intervening extern int days[] declaration; you're saying
"days is an array of some unknown size," even though the compiler will
be able to determine what the size is without that declaration.

"sizeof applied to incomplete type" means just what it says -- you're
trying to ask for the size of an object that at that point in time the
compiler doesn't know about.  Consider the two code fragments:

    int array[] = { 1, 2, 3 };
    int size = sizeof array;

and

    extern int array[];
    int size = sizeof array; /* error */

In the first, you're defining an array (and initializing it).  Even
though you don't specify the length of the array explicitly, the
compiler knows at that point how big the array will be.

The second fragment, however, will not compile.  Since array is being
declared externally as an array of some unknown size, the compiler can't
know its size, and so sizeof array is an error.

I suspect that if you remove the extern int array[] declaration, it will
work fine.  (Note it is totally unnecessary because array is already
well within scope.)

Also note that conio routines and stdio routines are not
interchangeable; you should either use all-conio or all-stdio, or you
can expect problems if you're not careful.  (That and there are more
than 10 months in the year.)

> Email appreciated.

[Posted and mailed.]

-- 
Erik Max Francis / email max AT alcyone DOT com / whois mf303 / icq 16063900
Alcyone Systems / irc maxxon (efnet) / finger max AT finger DOT alcyone DOT com
  San Jose, CA / languages En, Eo / web http://www.alcyone.com/max/
          USA / icbm 37 20 07 N 121 53 38 W / &tSftDotIotE
             \
            / What a crime to waste [youth] on children.
           / George Bernard Shaw

- Raw text -


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