Question about char pointers init to const strings

L

LaBird

Dear all,

I'd like to ask for the following C code segment:

#include <stdio.h>
int main()
{
char *a = "abc"; /* Line 1 */
printf("%s", a); /* Line 2 */
a[0] = '1'; /* Line 3 */
return 0;
}

The code compiles without warnings and errors,
runs ok until line 3 with "segmentation fault".
However, if Line 1 is changed to:

char a[] = "abc";

The program runs successfully. My question is:
Why char *a = "abc" is allowed, but then a[0] = '1'
will cause a segmentation fault?

Also, if Line 1 is changed to:
char *a = {'a', 'b', 'c', '\0'};

The program has compilation warnings, and the
execution will give segmentation fault on Line 2.
Why this form of initialization is not allowed?
Thanks!

Best Regards,
Benny (LaBird),
email: Remove all numerals to get the valid address.
 
H

hari4063

By standard (I think) if you have string char*="asdasda" it can be placed
in constant section of program, so changing that data will produce
segmentation. Some compilers put it even in code section.

char a[] = "asdasd" mean that "a" is array of chars finished by
null-termin. So, this string must not be in const section.

In

char *a = {'a', 'b', 'c', '\0'}

a is so-colled scalar and initialization of scalar object is only allowd
with one element.

I think that all gcc compilers move string to constant section (even
MinGW). With Win Borland C++, VC++ this is not case.
 
P

pete

char *a = "abc"; /* Line 1 */

In the above line, the string literal "abc",
converts to a pointer to a string. The pointer a,
is initialized with that value.
(sizeof a == sizeof &"abc")
Attempting to overwrite that string is undefined behavior.
Other references to "abc" in the same program,
may refer to the same string or different strings.
char a[] = "abc";

In the above line, a is array of 4 char which is initialized
with the values of 'a', 'b', 'c' and '\0'.
(sizeof a == sizeof "abc")
char *a = {'a', 'b', 'c', '\0'};

That doesn't mean anything in C.
 
F

Flash Gordon

Dear all,

I'd like to ask for the following C code segment:

#include <stdio.h>
int main()
{
char *a = "abc"; /* Line 1 */
printf("%s", a); /* Line 2 */
a[0] = '1'; /* Line 3 */
return 0;
}

The code compiles without warnings and errors,
runs ok until line 3 with "segmentation fault".
However, if Line 1 is changed to:

char a[] = "abc";

The program runs successfully. My question is:
Why char *a = "abc" is allowed, but then a[0] = '1'
will cause a segmentation fault?

<snip>

We've only just been discussing an almost identical question. The simple
answer is DON'T MODIFY STRING LITERALS. Also, your two definitions of a
are completely different, one is a pointer the other is an array.
 
P

pete

pete said:
In the above line, the string literal "abc",
converts to a pointer to a string. The pointer a,
is initialized with that value.
(sizeof a == sizeof &"abc")

I think that should be:
(sizeof a == sizeof &*"abc")
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,147
Messages
2,570,835
Members
47,382
Latest member
MichaleStr

Latest Threads

Top