473,386 Members | 1,864 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Can't figure this out

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
10 1818

"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
"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
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
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
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
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
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
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
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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Richard | last post by:
http://www.dyn-web.com/dhtml/scroll/ This script does what I am looking for. I can change the various variables to suit my needs except for one. The javascript based scrollbar locks into one...
102
by: Skybuck Flying | last post by:
Sometime ago on the comp.lang.c, I saw a teacher's post asking why C compilers produce so many error messages as soon as a closing bracket is missing. The response was simply because the compiler...
3
by: Brian Blais | last post by:
Hello, I have an odd kind of Heisenbug in what looks like a pretty simple program. The program is a progress bar code I got at the Python Cookbook: ...
1
by: Rich | last post by:
Hello, I have created icons before with icon editor that comes with VS2003 without any issues. Today I created another icon for my VB.Net app. But this .ico file is not displaying the figure...
8
by: nobrow | last post by:
Okay ... Im out of practice. Is it not possible to have a 2D array where each column is of a different type, say an int and a struct*? The following seg faults for me and I cant figure out what I...
0
by: Steve | last post by:
I have a gridview which uses an objectdatasource for its select and delete. The delete command uses the function below. The delete itself works but the extra logic which requires parameters...
9
by: Tony Girgenti | last post by:
Hello. I'm developing and testing a web application using VS.NET 2003, VB, .NET Framework 1.1.4322, ASP.NET 1.1.4322 and IIS5.1 on a WIN XP Pro, SP2 computer. I'm using a web form. For a...
1
by: Leonnhy | last post by:
HI, I'm a beginner in using MS Access. What's the code for the bring forward figure in current sales report I create as follows:- Once I click on the sales report, it will show me the date range...
13
by: paquer | last post by:
Ok' I have 2 tables. The first being my main table where each record has a 'FigureID' txt box. (Indexed - No duplicates) The second being my sub table where reporting is entered for these...
0
by: Frank | last post by:
Hi, I use rpy to plot functions and have the following problem. When I execute the following code line by line (start python and then execute line by line) the resulting figure looks as it...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.