By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,389 Members | 1,844 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,389 IT Pros & Developers. It's quick & easy.

Can't figure this out

P: n/a
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!

---
Joseph
Nov 13 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a

"Jeff" <ag*******@netcourrier.com> wrote in message
news:6c**************************@posting.google.c om...
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
Nov 13 '05 #2

P: n/a
"Allan Bruce" <al*****@TAKEAWAYf2s.com> wrote in
news:bp**********@news.freedom2surf.net:
int main (void)
{
char* s = "CAT";

*(s+1) = 'B';
BANG! You may have just attempted to write to a string literal in ROM,
FLASH, TLB protected RAM, etc.
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.

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?

--
- Mark ->
--
Nov 13 '05 #3

P: n/a
ag*******@netcourrier.com (Jeff) wrote:
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

--
Irrwahn
(ir*******@freenet.de)
Nov 13 '05 #4

P: n/a
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.


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

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

--
Er*********@sun.com
Nov 13 '05 #5

P: n/a
ag*******@netcourrier.com (Jeff) wrote in news:6c79eab5.0311180700.75725fb8
@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
Nov 13 '05 #6

P: n/a
ag*******@netcourrier.com (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

--
Derk Gwen http://derkgwen.250free.com/html/index.html
I have no idea what you just said.
I get that alot.
Nov 13 '05 #7

P: n/a
In <Xn********************************@130.133.1.4> "Mark A. Odell" <no****@embeddedfw.com> writes:
news:bp**********@news.freedom2surf.net:
Just what am I doing wrong here? Just for information this 'code' was
compiled using GCC under cygwin, on a windows 98 machine.

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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #8

P: n/a
On 18 Nov 2003 07:00:29 -0800, in comp.lang.c ,
ag*******@netcourrier.com (Jeff) wrote:
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.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
Nov 13 '05 #9

P: n/a
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.

---
Joseph
Nov 13 '05 #10

P: n/a

"Jeff" <ag*******@netcourrier.com> wrote in message
news:6c**************************@posting.google.c om...
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
Nov 13 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.