[comp.sys.mac.programmer] LightSpeed C bug

s170@tank.uchicago.edu (harmon g washington) (08/03/89)

In Lightspeed C v3.0 a program I wrote contains the standard C programming language construct:
	char a[] = "Hello, World";
This is the definition of a constant string.  All other C compilers I have used this statement is legal;  it is in K&R;  It is legal in MPW C v3.0;  ... But in Lightspeed C I get the error "required erray bounds missing".  What is wrong?  
Does not LSC recognize this?  If not why?  How can one get around this?  A related problem I would like to do is:
	Str255 a[=] = "\pHello, World";
for Pascal strings used in ToolBox routines.  

sdh@wind.bellcore.com (Stephen D Hawley) (08/03/89)

In article <4807@tank.uchicago.edu> s170@tank.uchicago.edu (harmon g washington) writes:
>In Lightspeed C v3.0 a program I wrote contains the standard C programming language construct:
>	char a[] = "Hello, World";
>This is the definition of a constant string.  All other C compilers I have used this statement is legal;  it is in K&R;  It is legal in MPW C v3.0;  ... But in Lightspeed C I get the error "required erray bounds missing".  What is wrong?  
>Does not LSC recognize this?  If not why?  How can one get around this?  A related problem I would like to do is:
>	Str255 a[=] = "\pHello, World";
>for Pascal strings used in ToolBox routines.  

Ok, I know what your problem is.  It was a little tough to check because you
provided no context.  Here is a program:

#include <stdio.h>

char a[] = "Hello, World.";

main()
{
	static char b[] = "Buffers for breakfast, Billy!";
	char c[] = "Goodbye, cruel world.";

	printf("foo");
	bar();
}

bar()
{
	static int i;  /* static variables are zero by default */

	if (i == 0) {
		i = 1;
		printf("bar");
		main();
	}
}

The first definition (a) works because it is a global variable whose value
is known at compile time.  It gets initialized at compile time and is stuck
in your data segement.  Hunky dorey.  The second definition (b) also works.
It is a static variable, also initialized only at compile time.  The third
definition (c) will not compile.  c is an automatic variable.  This means
that the storage space for it gets allocated on the stack.  This an important
point.  Say it again.  The storage space gets allocated on the stack.
If you try to initialize it, you are asking the compiler to make space for
something with a dynamic size on the stack.  How is it going to be initialized?
How will the data be copied there?  What if the string is gigantic?  Won't
that be painful?

For those who are into the esoterica, I added the function bar to show that
main() is just a normal function (just a special name), that can be called
from anywhere else.

So all this wind comes down to:  use static definitions if it is local to
the function (or file).  Don't use static if you intend the scope to be
global.

Steve Hawley
sdh@flash.bellcore.com
"Up is where you hang your hat."
	--Jim Blandy, computer scientist

awd@dbase.UUCP (Alastair Dallas) (08/04/89)

In article <4807@tank.uchicago.edu>, s170@tank.uchicago.edu (harmon g washington) writes:
> In Lightspeed C v3.0 a program I wrote contains the standard C programming
> language construct:
> 	char a[] = "Hello, World";
> This is the definition of a constant string.  All other C compilers I have
> used this statement is legal;  it is in K&R;  It is legal in MPW C v3.0;  ...
> But in Lightspeed C I get the error "required erray bounds missing".
> What is wrong?  

You're right, of course--this is valid C.  K&R p.109 (s5.9) and p.199 (s8.6).
Is it possible you actually typed:

	char *a[] = "Hello, World";

That would require braces.  In any event:

	char *a = "Hello, World"; 	or
	Str255 a = "\pHello, World";

should work.  I would check all four on THINK C, but I'm at work and its
at home.

Hope it helps.

/alastair/