[comp.sys.mac.programmer] String Initialization Problem in LSC

lesh@oscar.ccm.udel.edu (12/22/88)

I'm having a problem with initializing string constants in LightSpeed C.
I have generated a short program that exhibits the problem and is presented
below.

---------------------------------------------------------------------------
File: Test.c
---------------------------------------------------------------------------
#include <stdio.h>

extern char *emptyString;
extern char *testString1,*testString2;

main()
{
	printf("%08lx: '%s'\n",emptyString,emptyString);
	printf("%08lx: '%s'\n",testString1,testString1);
	printf("%08lx: '%s'\n",testString2,testString2);
}

---------------------------------------------------------------------------
File: Test Library.c
---------------------------------------------------------------------------
char *emptyString="";
char *testString1="Hello1";
char testString2[]="Hello2";
---------------------------------------------------------------------------

If you compile these two files in a project with MacTraps and stdio you
will get the following output before the program hangs.

000f72b6: ''
000f72b8: 'Hello1'
48656c6c: '|

In the second edition of "C: A Reference Manual" by Harbison and Steele,
they claim that strings can be intializes as follows:

static char x[5]="ABCD";
static char str[]="ABCDEF";

So why doesn't this program compile correctly in LSC 3.01?

As is turns out, if you modify the file Test.c as follows and remove the
Test Library.c file from the project you will get the correct results.

---------------------------------------------------------------------------
File: Test.c
---------------------------------------------------------------------------
#include <stdio.h>

char *emptyString="";
char *testString1="Hello1";
char testString2[]="Hello2";

main()
{
	printf("%08lx: '%s'\n",emptyString,emptyString);
	printf("%08lx: '%s'\n",testString1,testString1);
	printf("%08lx: '%s'\n",testString2,testString2);
}
---------------------------------------------------------------------------

Thanks,

Richard Lesh
CCM
University of Delaware
Newark, DE  19716
302-451-1519

Internet: LESH@OSCAR.CCM.UDEL.EDU
  BITNET: LESH%OSCAR.CCM.UDEL.EDU@CUNYVM
 ARPANET: LESH@OSCAR.CCM.UDEL.EDU