Can't figure this out

J

Jeff

Hello everybody,

I just have a little problem, and I just can't figure out what it is:

[gongds @ labtherm][15:52:28]$ cat test.c
#include <stdio.h>
int main (void)
{
char* s = "CAT";

*(s+1) = 'B';
printf("%s", s);

return (0);
}
[gongds @ labtherm][15:52:32]$ gcc -o test test.c
[gongds @ labtherm][15:52:40]$ ./test
Segmentation fault (core dumped)
[gongds @ labtherm][15:52:42]$

Just what am I doing wrong here? Just for information this 'code' was
compiled using GCC under cygwin, on a windows 98 machine.

Thank-you for any help,
Cheers!
 
A

Allan Bruce

Jeff said:
Hello everybody,

I just have a little problem, and I just can't figure out what it is:

[gongds @ labtherm][15:52:28]$ cat test.c
#include <stdio.h>
int main (void)
{
char* s = "CAT";

*(s+1) = 'B';
printf("%s", s);

return (0);
}
[gongds @ labtherm][15:52:32]$ gcc -o test test.c
[gongds @ labtherm][15:52:40]$ ./test
Segmentation fault (core dumped)
[gongds @ labtherm][15:52:42]$

Just what am I doing wrong here? Just for information this 'code' was
compiled using GCC under cygwin, on a windows 98 machine.

Thank-you for any help,
Cheers!

I think the problem is that you are declaring your pointer to point to a
string literal which you cannot write to, so when you try you are getting a
core dump.
Allan
 
M

Mark A. Odell

BANG! You may have just attempted to write to a string literal in ROM,
FLASH, TLB protected RAM, etc.

The compiler and platform should never matter for ISO C code.
I think the problem is that you are declaring your pointer to point to a
string literal which you cannot write to, so when you try you are
getting > a core dump.

Correct. You cannot do what the OP is trying to do and expect it to work.
To the OP: What if the compiler placed "CAT" into ROM or FLASH?
 
I

Irrwahn Grausewitz

I just have a little problem, and I just can't figure out what it is:

#include <stdio.h>
int main (void)
{
char* s = "CAT";

*(s+1) = 'B';
printf("%s", s);

return (0);
}
Segmentation fault (core dumped)

Just what am I doing wrong here? Just for information this 'code' was
compiled using GCC under cygwin, on a windows 98 machine.

You are not allowed to change string literals; they may reside in
read-only memory. To make your code work, change the declaration/
initialization of s to:

char s[] = "CAT";

s is now declared as an array of four(!) characters and initialized
with 'C''A''T''\0' (as opposed to being declared as a character
pointer pointing to a string literal in your original code).

HTH
Regards
 
E

Eric Sosman

Jeff said:
Hello everybody,

I just have a little problem, and I just can't figure out what it is:

[gongds @ labtherm][15:52:28]$ cat test.c
#include <stdio.h>
int main (void)
{
char* s = "CAT";

*(s+1) = 'B';
printf("%s", s);

return (0);
}
[gongds @ labtherm][15:52:32]$ gcc -o test test.c
[gongds @ labtherm][15:52:40]$ ./test
Segmentation fault (core dumped)
[gongds @ labtherm][15:52:42]$

Just what am I doing wrong here? Just for information this 'code' was
compiled using GCC under cygwin, on a windows 98 machine.

This is Question 16.6 in the comp.lang.c Frequently
Asked Questions (FAQ) list

http://www.eskimo.com/~scs/C-faq/faq.html
 
I

Ian Woods

(e-mail address removed) (Jeff) wrote in @posting.google.com:
Hello everybody,

I just have a little problem, and I just can't figure out what it is:

[gongds @ labtherm][15:52:28]$ cat test.c
#include <stdio.h>
int main (void)
{
char* s = "CAT";

s pointer to a string literal.
*(s+1) = 'B';

You're trying to modify the string literal. That's undefined behaviour
IIRC, hence the nasal demons.
printf("%s", s);

return (0);
}

Just what am I doing wrong here? Just for information this 'code' was
compiled using GCC under cygwin, on a windows 98 machine.

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

Ian Woods
 
D

Derk Gwen

(e-mail address removed) (Jeff) wrote:
# Hello everybody,
#
# I just have a little problem, and I just can't figure out what it is:
#
# [gongds @ labtherm][15:52:28]$ cat test.c
# #include <stdio.h>
# int main (void)
# {
# char* s = "CAT";
#
# *(s+1) = 'B';
# printf("%s", s);
#
# return (0);
# }
# [gongds @ labtherm][15:52:32]$ gcc -o test test.c
# [gongds @ labtherm][15:52:40]$ ./test
# Segmentation fault (core dumped)
# [gongds @ labtherm][15:52:42]$
#
# Just what am I doing wrong here? Just for information this 'code' was
# compiled using GCC under cygwin, on a windows 98 machine.

s points to a literal string. Some implementations permit literal strings
to be modified; others out literal strings in an unwritable page, and
attempts to write to them will get segmentation faults or other memory
related errors. If you want to be able to modify characters of s without
making the string longer, do

char s[] = "CAT";

Enough writable space will be allocated to s to hold the string, and
the string will be copied into the space.

(cd /tmp
cat <<':eof' >t.c
#include <stdio.h>
int main (int N,char **P)
{
char s[] = "CAT";

*(s+1) = 'B';
printf("%s", s);

return (0);
}
:eof
cc t.c; /tmp/a.out)

CBT
 
D

Dan Pop

In said:
The compiler and platform should never matter for ISO C code.

For ISO C code that works. If it doesn't, specifying this kind of
information can be helpful in identifying the origin of the problem.
Correct. You cannot do what the OP is trying to do and expect it to work.
To the OP: What if the compiler placed "CAT" into ROM or FLASH?

OTOH, since we know the OP's compiler, we can also recommend
-fwritable-strings for code that, for one reason or another, *must* be
able to write into string literals.

This is, of course, to be avoided in new code, but there is still old
code floating around that relies on writable string literals, because
this is how they were before ANSI C. Most commercial compilers simply
don't take advantage of the optimisations allowed by the standard in this
area, in order to avoid breaking such code. gcc's solution was to
provide -fwritable-strings and use read-only string literals by default,
which is a good thing, because it prevents beginners from writing
broken code that works by accident (the typical beginner doesn't
use -fwritable-strings).

Dan
 
M

Mark McIntyre

On 18 Nov 2003 07:00:29 -0800, in comp.lang.c ,
Hello everybody,

I just have a little problem, and I just can't figure out what it is:

char* s = "CAT";

s points to a string literal. These are nonmodifiable.
*(s+1) = 'B';

you tried to modify it - *bang*

This is a FAQ by the way - its well worth reading that doc before
posting here.
 
J

Jeff

Thank-you very much for all your help.

I am sorry for not having seen this in the FAQ. I had actually checked
the FAQ, but only in the pointer section, thus missing the crucial
question ;)

Thanks again.
 
M

Mike Wahler

Jeff said:
Thank-you very much for all your help.

I am sorry for not having seen this in the FAQ. I had actually checked
the FAQ, but only in the pointer section, thus missing the crucial
question ;)

Evidence for the oft given advice here:
Read The Entire FAQ. And even after that,
it's still typically helpful to review it
periodically. All of it. :)

-Mike
 

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

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top