array declaration vs. char pointer 
Author Message
 array declaration vs. char pointer

What is the difference between the following declarations?

char str[] = "hello world";

char *str = "hello world";

Does the compiler treat these two declarations any differently?



Sun, 22 Feb 1998 03:00:00 GMT  
 array declaration vs. char pointer

Quote:
>What is the difference between the following declarations?

>char str[] = "hello world";

The above creates a character array initialized to hold the string "hello world".
This string could be modified or replaced, as long as you stay inside the
length of the original string.

Quote:

>char *str = "hello world";

The above creates a character pointer that points to a const string.
A strong ANSI compiler would not allow you to change the string in
this case.  The pointer can be changed to point elsewhere.


Quote:

>Does the compiler treat these two declarations any differently?



Sun, 22 Feb 1998 03:00:00 GMT  
 array declaration vs. char pointer
: What is the difference between the following declarations?

: char str[] = "hello world";

: char *str = "hello world";

: Does the compiler treat these two declarations any differently?

Lets change some names to make it easier to talk about:

char str_ar[] = "hello world";

char *str_p = "hello world";

str_ar is a char array of size 12.  The compiler sizes the array to the
size of the initializer string.  The terminating '\0' is included in the
calculation of the size.

sizeof(str_ar) will evaluate to 12.  

str_p is a pointer to a char, initialized to point to the string literal
"hello world" which is wherever in memory your compiler likes to put string
literals.  This is possibly in the static data area, but possibly it's
elsewhere like in the text segment.  

The string literal is not necessarily writeable.  In other words,
the statement

        *str_p='H';

may produce the helpful message "Core dumped" or other behaviour.

sizeof(str_p) will evaluate to whatever the size of a char * is on the
system in question, and would be the same if the initialization was
"hello world" or "hi" or "Greetings O Planet Mine".

Try writing a wrapper for those declarations and look at them with a
de{*filter*}.

Regards,
jpc



Mon, 23 Feb 1998 03:00:00 GMT  
 array declaration vs. char pointer

Quote:

>What is the difference between the following declarations?

>char str[] = "hello world";

>char *str = "hello world";

>Does the compiler treat these two declarations any differently?

From the c.l.c faq:

1.32:   What is the difference between these initializations?

                char a[] = "string literal";
                char *p  = "string literal";

        My program crashes if I try to assign a new value to p[i].

A:      A string literal can be used in two slightly different ways.  As
        an array initializer (as in the declaration of char a[]), it
        specifies the initial values of the characters in that array.
        Anywhere else, it turns into an unnamed, static array of
        characters, which may be stored in read-only memory, which is
        why you can't safely modify it.  In an expression context, the
        array is converted at once to a pointer, as usual (see section
        6), so the second declaration initializes p to point to the
        unnamed array's first element.

        (For compiling old code, some compilers have a switch
        controlling whether strings are writable or not.)

        See also questions 1.31, 6.1, 6.2, and 6.8.

        References: K&R2 Sec. 5.5 p. 104; ANSI Sec. 3.1.4, Sec. 3.5.7;
        ISO Sec. 6.1.4, Sec. 6.5.7; Rationale Sec. 3.1.4; H&S Sec. 2.7.4
        pp. 31-2.

You might want to look through the faq.  It contains explinations for
many of the common gotcha's in c programming.___.
                                                |
                                               \|/
--
John R Buchan           -:|:-     Looking for that elusive FAQ?  ftp to:



Mon, 23 Feb 1998 03:00:00 GMT  
 array declaration vs. char pointer
 > What is the difference between the following declarations?
 > char str[] = "hello world";
 > char *str = "hello world";
 >
 > Does the compiler treat these two declarations any differently?
 >
Yes.  Pray read the FAQ.
--
dik t. winter, cwi, kruislaan 413, 1098 sj  amsterdam, nederland, +31205924098



Mon, 23 Feb 1998 03:00:00 GMT  
 array declaration vs. char pointer

Quote:

>What is the difference between the following declarations?

>char str[] = "hello world";

>char *str = "hello world";

>Does the compiler treat these two declarations any differently?

From the FAQ list, RTFM.mit.edu:/pub/usenet/comp.lang.c/* :

1.32:   What is the difference between these initializations?

                char a[] = "string literal";
                char *p  = "string literal";

        My program crashes if I try to assign a new value to p[i].

A:      A string literal can be used in two slightly different ways.  As
        an array initializer (as in the declaration of char a[]), it
        specifies the initial values of the characters in that array.
        Anywhere else, it turns into an unnamed, static array of
        characters, which may be stored in read-only memory, which is
        why you can't safely modify it.  In an expression context, the
        array is converted at once to a pointer, as usual (see section
        6), so the second declaration initializes p to point to the
        unnamed array's first element.

        (For compiling old code, some compilers have a switch
        controlling whether strings are writable or not.)

        See also questions 1.31, 6.1, 6.2, and 6.8.

        References: K&R2 Sec. 5.5 p. 104; ANSI Sec. 3.1.4, Sec. 3.5.7;
        ISO Sec. 6.1.4, Sec. 6.5.7; Rationale Sec. 3.1.4; H&S Sec. 2.7.4
        pp. 31-2.

Hope that helps...

Maurizio Loreti                       http://mvxpd5.pd.infn.it/wwwcdf/mlo.html



Mon, 23 Feb 1998 03:00:00 GMT  
 array declaration vs. char pointer
|> What is the difference between the following declarations?
|>
|> char str[] = "hello world";

This is modifiable...I.e. str[5] = '-'; would be legal.
(I'm too tired to think of a better example)

|> char *str = "hello world";

This cannot be modified and should be treated only as a constant
string.

|> Does the compiler treat these two declarations any differently?

Which compiler is 'the' compiler?
--
"If it wasn't for C, we would be using BASI, PASAL, and OBOL."




Tue, 24 Feb 1998 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. A char pointer (char *) vs. char array question

2. arrays and pointers, char * vs. char [], distinction

3. char pointer to 2D char array

4. char pointers vs. int pointers

5. pointer to array vs pointer

6. char[] vs char * vs #defines for constant strings

7. array and pointer declarations, faq 6.4

8. Declaration of 2D array with pointer ???

9. defining declarations vs. referencing declarations

10. Differences between char array[SIZE] and char *array

11. Comparison with pointer to pointer to char, and char

12. string vs array of char?

 

 
Powered by phpBB® Forum Software