Re: I'm sorry...



>>>>> "S" == sml13  <sml13@cornell.edu> writes:

S> Good point...BUT, this dies too: 

S>  char* var = "hey you";

That's also a constant string.  The C standard allows implementations
to put stuff like that in read-only memory and the like.  (I'd quote
you chap and verse, but it's at work.)  The above statement
initializes a char pointer, which points at the constant string
somewhere in memory.

The line

char var[] = "hey you"; 

will act the same in most cases, but here you're allocating storage,
which is being initialized from a string literal.  Your line is
allocating a pointer, not an array, which points at a string literal.

Relevant sections of the C9x draft (all I have here, but the numbers
are usually close to the C89 ISO document are 6.1.4  String literals
and 6.5.8 Initialization, which has an example which explains the
difference well:

         7.  The declaration

                     char s[] = "abc", t[3] = "abc";

             defines ``plain'' char array objects  s  and  t  whose
             elements   are   initialized   with  character  string
             literals.  This declaration is identical to

                     char s[] = { 'a', 'b', 'c', '\0' },
                          t[] = { 'a', 'b', 'c' };

             The contents of the arrays  are  modifiable.   On  the
             other hand, the declaration

                     char *p = "abc";

             defines p  with  type  ``pointer  to  char''  that  is
             initialized to point to an object with type ``array of
             char'' with length 4 whose  elements  are  initialized
             with  a  character  string  literal.  If an attempt is
             made to use p to modify the contents of the array, the
             behavior is undefined.


-- 
Alan Shutko <ats@acm.org> - By consent of the corrupted
Seattle is so wet that people protect their property with watch-ducks.



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]