string assignment in C

Mark Dornfeld romwa at gpu.utcs.toronto.edu
Wed Oct 12 04:37:28 AEST 1988


I've been reading a book on /rdb recently.  In the book they
have a small example where they assign strings.

	char *p1="first";
	char *p2;

	main( argc, argv )
	int argc;
	char *argv[];

	{
	  p2 = " is:";
	}

The assignment to p1 makes sense to me, because the compiler
could set aside the size of the string being assigned.  The
second case baffles me.  I always thought that you had to give
a string a "maximum size" and then use strcpy or sprintf for
assignment.  Isn't the assignment of p2 a dangerous thing to
do since the compiler has (presumably) only left enough space
for the pointer and not for the string.  I tried this example
out on QuickC and everything worked.  i.e. printf'ing p2 gives
'is:'.

Could someone please shed some light on this for me.  Could
you also please respond via e-mail, since I am borrowing someone's
account in order to post this.

advTHANKSance

Pavneet Arora
...!utgpu!rom!pavneet

Royal Ontario Museum
100 Queen's Park
Toronto, Ontario
M5S 2C6
(416) 585-5626



More information about the Comp.lang.c mailing list