LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 09-29-2004, 07:13 AM   #1
dileepkk
Member
 
Registered: Jul 2004
Posts: 42

Rep: Reputation: 15
How to modify an initialised string


Hi,

here is small segment of code:
Code:
#include <stdio.h>
main()
{
char *src="linux";
*(src+0)='L';
printf("%s",src);
}
Iam getting SEGMENTATION FAULT because of the statement
Code:
*(src+0)='L';
why it is so ???
And, how to change the content of the above string??

Thanks in advance

Dileep

Last edited by dileepkk; 09-29-2004 at 07:17 AM.
 
Old 09-29-2004, 07:31 AM   #2
m00t00
Member
 
Registered: Sep 2004
Distribution: Slackware 10, Gentoo
Posts: 292

Rep: Reputation: 30
you're derefrencing it. meaning when you change src[0] to L, you're actually changing the first part of the address it points to. when you call printf, printf tries to print from some nonexistant address, and you segfault.

try changing it to:

src[0]='L'
 
Old 09-29-2004, 12:14 PM   #3
wpyh
Member
 
Registered: Jun 2004
Location: Beijing
Distribution: Slackware 9.1 but FUBAR with packages I compile myself, and OpenBSD (not exactly a distro) on QEMU
Posts: 153

Rep: Reputation: 35
#include <stdio.h>
main()
{
char *src="linux";
src[0]='L';
printf("%s",src);
}

This code gives me a segfault...
 
Old 09-29-2004, 02:28 PM   #4
proudclod
Member
 
Registered: May 2004
Distribution: Fedora Core 2
Posts: 381

Rep: Reputation: 30
erm, what are you compiling it with?

If it's strict it might be worth adding in

return 0;
 
Old 09-29-2004, 07:32 PM   #5
wpyh
Member
 
Registered: Jun 2004
Location: Beijing
Distribution: Slackware 9.1 but FUBAR with packages I compile myself, and OpenBSD (not exactly a distro) on QEMU
Posts: 153

Rep: Reputation: 35
gcc 3.4.2

Well, this _does_ segault too:
#include <stdio.h>
int main()
{
char *src="linux";
src[0]='L';
printf("%s",src);
return 0;
}
 
Old 09-29-2004, 07:36 PM   #6
wpyh
Member
 
Registered: Jun 2004
Location: Beijing
Distribution: Slackware 9.1 but FUBAR with packages I compile myself, and OpenBSD (not exactly a distro) on QEMU
Posts: 153

Rep: Reputation: 35
A preinitialized string is a constant. At least, we shouldn't modify it because it points to a data area that's not writable by us (I think). This works:

#include <stdio.h>
main()
{
char *src = malloc(6);
strcpy(src, "linux");
printf("%s\n", src);
*(src+0)='L';
printf("%s\n", src);
return 0;
}

So it's not actually a problem with Linux, but with the program itself..
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to convert Assembly code to "C" source code ssg14j Programming 2 08-01-2005 12:48 PM
Does anyone know where the source code is? Echo Kilo Mandriva 8 12-22-2004 12:00 PM
Config problem with kernel source code Astrojax Linux - Newbie 1 08-23-2004 02:39 PM
Source code hey_joe Linux - Newbie 1 03-14-2004 01:06 PM
Problem in compiling source code JIV Linux - Software 1 02-14-2004 03:11 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 12:40 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration