473,659 Members | 2,666 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1844

"Jeff" <ag*******@netc ourrier.com> wrote in message
news:6c******** *************** ***@posting.goo gle.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+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*****@TAKEAW AYf2s.com> wrote in
news:bp******** **@news.freedom 2surf.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*******@netco urrier.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*******@free net.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*******@netco urrier.com (Jeff) wrote in news:6c79eab5.0 311180700.75725 fb8
@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*******@netco urrier.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****@embedde dfw.com> writes:
news:bp******* ***@news.freedo m2surf.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*******@netco urrier.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.c om/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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1050
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 position and I can't figure out how to move it to the appropriate location. If anyone can figure it out, I'd appreciate it.
102
5649
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 can't tell where a bracket is missing.... a few weeks have past, I requested a feature for the delphi ide/editor "automatic identation of code in begin/end statements etc" and today when I woke up I suddenly released a very simple solution for this...
3
1353
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: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/168639 (including the code below)
1
1846
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 that I drew on it in the editor (32x32). When I add the icon to the project, the icon does not display the figure on the form, but it does display in the Alt-Tab box, and it displays in the Project Properties page. Also, the icon does not display...
8
3200
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 need to change. Thanks. #include <malloc.h> #include <string.h>
0
2658
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 AccountDebitID and AccountCreditID does not work. 'Delete a transaction Sub deletetrans(ByVal transactionid As Integer, ByVal transactionamount
9
2117
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 datagrid control, i used the Caption property. It displays fine on the datagrid and allows me to run the program. But, when i view the HTML for the web form, it shows this error at the
1
2230
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 of report need to be entered, then report will show total sales figure for current date range selected plus bring forward figure as well, however, I didn't know how to enter the code on the "bring forward figure" in order to get carried forward...
13
8288
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 Figures. Sometimes more than one at a time. I have 4 'FigureID' txt boxes on this subtable.(All Indexed - Duplicates OK) When I set the Master/Child link for the first FigureID on the Subtable I have no problems.
0
1124
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 should. However, when I put these lines in a script and execute the script the figure appears for half a second but then crashes. Using pylab.show() at the end of the script prevents the crash but now the window is no longer refreshed (e.g., changing...
0
8427
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8330
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8746
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8626
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7355
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6178
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4334
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.