char a[] = "abc"; char* a = "abc"; 
Author Message
 char a[] = "abc"; char* a = "abc";

I'm trying to figure out the difference in these
2 declarations. I first thought they were the same,
but after trying to manipulate the strings with a char*
I get different results:

#include <stdio.h>
#include <string.h>

int main(int argc, char** argv)
{
    /* why does this succeed */
    char a[] = "mary had a wee lamb";

    /* but this segfaults */
    /* char* a = "mary had a wee lamb"; */

    char* b;
    b = strstr(a, "wee");
    *b = '\0';
    puts(a);
    return 0;

Quote:
}

Here is the simple gcc Makefile I
compiled with, for completeness:

ptest : ptest.o
        cc -o ptest ptest.o
ptest.o : ptest.c
        cc -c -ansi -Wall -pedantic ptest.c

--
Sean E Dolan
Resistance is futile...



Sat, 24 Jul 2004 13:19:36 GMT  
 char a[] = "abc"; char* a = "abc";
Quote:

> I'm trying to figure out the difference in these
> 2 declarations. I first thought they were the same,
> but after trying to manipulate the strings with a char*
> I get different results:

> #include <stdio.h>
> #include <string.h>

> int main(int argc, char** argv)
> {
>     /* why does this succeed */
>     char a[] = "mary had a wee lamb";

>     /* but this segfaults */
>     /* char* a = "mary had a wee lamb"; */

>     char* b;
>     b = strstr(a, "wee");
>     *b = '\0';

The problem is here. `b' now points to a char within a literal string,
which is not necessarily mutable (taking the commented out definition of
`a'). In the `char a[] = ...' case, it's pointing into a `true' array,
which is mutable.
Quote:
>     puts(a);
>     return 0;
> }

HTH,
--ag

--
Artie Gold  --  Austin, TX

"May you keep turning the pages. And may the book never end."



Sat, 24 Jul 2004 13:39:25 GMT  
 char a[] = "abc"; char* a = "abc";
On Tue, 05 Feb 2002 05:19:36 GMT, Sean E Dolan


Quote:
> I'm trying to figure out the difference in these
> 2 declarations. I first thought they were the same,
> but after trying to manipulate the strings with a char*
> I get different results:

In the first case ("character array"), you declared an array of
characters, allocated 20 bytes, and put the characters 'M',
'a', 'r', 'y',  ' ', 'h', 'a', 'd', ' ', 'a', ' ', 'w', 'e',
'e', ' ', 'l', 'a', 'm', 'b',`\0', in the 0th to the 19th
positions of the array. It is your array. You can modify any
character in the array, as long as you work within the limits.
(i.e., subscripts 0-19). And the address of the array is
constant.

In the second case ("string literal"), the compiler finds the
above 20 consecutive characters from memory, or allocates some
space reserved for it and put those characters in it. That
space may be used for other purposes too; so, you are not
supposed to modify it (undefined behavior), even though you can
read it. And, it stored the address of the first character in
the pointer you provided.

Now you know why the first one segfaults (it may not
necessarily) while the second one doesn't.

Quote:

> #include <stdio.h>
> #include <string.h>

> int main(int argc, char** argv)
> {
>     /* why does this succeed */
>     char a[] = "mary had a wee lamb";

>     /* but this segfaults */
>     /* char* a = "mary had a wee lamb"; */

>     char* b;
>     b = strstr(a, "wee");
>     *b = '\0';

This is undefined behavior if a was pointing to a string
literal. It is OK if it is a character array.

- Umesh

--

Umesh P Nair
Remove 'z's from my e-mail ID



Sat, 24 Jul 2004 13:48:06 GMT  
 char a[] = "abc"; char* a = "abc";
On Tue, 05 Feb 2002 05:48:06 GMT, Umesh P Nair


Quote:
> Now you know why the first one segfaults (it may not
> necessarily) while the second one doesn't.

The other way round. The first one doesn't, while the second
one may. Sorry for the mistake.

- Umesh

--

Umesh P Nair
Remove 'z's from my e-mail ID



Sat, 24 Jul 2004 13:54:16 GMT  
 char a[] = "abc"; char* a = "abc";
On Tue, 05 Feb 2002 00:19:36 -0500, Sean E Dolan

Quote:
> I'm trying to figure out the difference in these
> 2 declarations. I first thought they were the same,
> but after trying to manipulate the strings with a char*
> I get different results:

> #include <stdio.h>
> #include <string.h>

> int main(int argc, char** argv)
> {
>     /* why does this succeed */
>     char a[] = "mary had a wee lamb";

The definition above creates an array of 20 characters, initialized
with the 19 visible characters in your string literal, plus a
terminating '\0'.  The contents of these 20 characters can be modified
at will.

Quote:
>     /* but this segfaults */
>     /* char* a = "mary had a wee lamb"; */

The definition above contains a string literal 20 characters long
(including the '\0').  This creates an unnamed array of characters
somewhere.  It also creates a pointer to char named a and stores the
address of the first character of the string literal in a.

The pointer a can be modified to point to other chars, or it can be
set to NULL.  The string literal itself is a peculiar thing in C.
Attempting to modify one causes undefined behavior.  Many compilers
for platforms with hardware memory protection like Windows, Linux, and
other UNIX variants, place string literals in an area of memory that
is marked read only as far as the program is concerned.

Generating a seg fault is one possible consequence of undefined
behavior.

Quote:
>     char* b;
>     b = strstr(a, "wee");
>     *b = '\0';
>     puts(a);
>     return 0;
> }

> Here is the simple gcc Makefile I
> compiled with, for completeness:

> ptest : ptest.o
>         cc -o ptest ptest.o
> ptest.o : ptest.c
>         cc -c -ansi -Wall -pedantic ptest.c

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq


Sat, 24 Jul 2004 13:58:00 GMT  
 char a[] = "abc"; char* a = "abc";

Quote:

>     /* why does this succeed */
>     char a[] = "mary had a wee lamb";

>     /* but this segfaults */
>     /* char* a = "mary had a wee lamb"; */

I'm surprised no-one's mentioned this, but:

http://www.eskimo.com/~scs/C-faq/q1.32.html
http://www.eskimo.com/~scs/C-faq/q16.6.html

--
David Scarlett
dscarlett [AT] optushome [DOT] com [DOT] au

"Damn it, Kif, where's the little umbrella? That's what makes it a
 scotch on the rocks!"
        -Capt. Zapp Brannigann, Futurama



Sat, 24 Jul 2004 15:13:45 GMT  
 char a[] = "abc"; char* a = "abc";
Thanks everyone for the replies.
And the FAQ link

--
Sean E Dolan
Resistance is futile...



Sat, 24 Jul 2004 15:11:39 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. char foo[]=""; vs char* foo="";

2. Difference between "char *arr" and "char arr[]"

3. difference between "char*" and "const char*"

4. decode/unescape "%uxxxx" char in VC++

5. char a[] = ("x");

6. Any != between (char) NULL and ""

7. is this allowed char *name = "bryan"

8. Delete "/" from char pointer

9. What is "unsigned char data[0];"

10. Initialize a char* with "a string"???

11. How do YOU pronounce "char"

12. "cast"ing of char *

 

 
Powered by phpBB® Forum Software