Article View: comp.sys.atari.st
Article #6620Re: C question
From: Ron DeBlock
Date: Wed, 06 Dec 1989 17:45
Date: Wed, 06 Dec 1989 17:45
56 lines
2303 bytes
2303 bytes
Someone asked why
main()
{
char fred[] = "hello";
....
won't work, and a bunch of answers followed. Some made no sense, others
were close, but I haven't seen the complete answere yet. So here it is:
The error from the compiler on this line is probably something like:
"No initialization of automatic agragate."
This means that an aggragate structure (an arry, struct or union) that is
automatically allocat cannot be pre-initialized. Automatics are any
variables declared INSIDE of a function (this excludes the functions
arguments). For non-aggragate variables, (int, *, cha the compiler
will generate storage and code to initialize. It does not do so for
aggragates.
The solution is to make the variable non-automatic. Someone mentioned
declaring the array as
static char fred[]
which will work just fine. Global variables are also not automatic,
so moving the declaration outside of main() will also work.
C provides the keyword "auto" to make variables automatic. I've
never seen it used, since it is only valid within functi (or block)
scope and it is the default.
The "static" keyword has an interesting side effect: a global symbol
declared as static only has file scope. This allows you to create
global vaiables and functions which are visible only within a file:
static int foo;
int bar;
static int bas()
{
...
}
int baf()
{
...
}
baf() and bar may be declareas extern in other files and used normally.
However, foo and bas() CANNOT be referenced outside of the file! This is
sometimes useful if you want to be sure that certain variables or functions
cannot be accessed from other code.
--
Ron DeBlock N2JSO
Net: ...!rutgers!galaxy!argus
US Mail: 42 Davis Street, Phillibsburg, NJ 08865 USA
Technical Details
Message-ID:
<1381@argus.UUCP>Path:
arc1-ca.pugleaf.net!utzoo!attcan!utgpu!jarvis.csri.toronto.edu!cs.utexas.edu!uwm.edu!psuvax1!rutgers!galaxy.rutgers.edu!argus!ronReferences:
<8912050802.AA12717@ucbvax.Berkeley.EDU> <875@lzaz.ATT.COM> <74734@tut.cis.ohio-state.edu>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!attcan!utgpu!jarvis.csri.toronto.edu!cs.utexas.edu!uwm.edu!psuvax1!rutgers!galaxy.rutgers.edu!argus!ron From: ron@argus.UUCP (Ron DeBlock) Newsgroups: comp.sys.atari.st Subject: Re: C question Summary: static, extern, auto Message-ID: <1381@argus.UUCP> Date: 6 Dec 89 17:45:24 GMT References: <8912050802.AA12717@ucbvax.Berkeley.EDU> <875@lzaz.ATT.COM> <74734@tut.cis.ohio-state.edu> Reply-To: ron@argus.UUCP (Ron DeBlock) Organization: NJ Inst of Tech, Newark NJ Lines: 56