Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > String/Array Question

Reply
Thread Tools

String/Array Question

 
 
Matthew Alton
Guest
Posts: n/a
 
      04-25-2004
Why does this code generate a SIGSEGV?

#include <stdio.h>

int
main(void)
{
char *s = "ABC";

s[1] = 'X';

return(0);
}

while this does not:

#include <stdio.h>

int
main(void)
{
char s[] = "ABC";

s[1] = 'X';

return(0);
}


The *s declaration followed by a reference to s[-1] is used in
FreeBSD's strsep(3). It dumps core on Red Hat Linux 7.x and NetBSD
1.6.1.
 
Reply With Quote
 
 
 
 
Irrwahn Grausewitz
Guest
Posts: n/a
 
      04-25-2004
http://www.velocityreviews.com/forums/(E-Mail Removed) (Matthew Alton) wrote:
>Why does this code generate a SIGSEGV?


> char *s = "ABC";
> s[1] = 'X';


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

Regards
--
Irrwahn Grausewitz ((E-Mail Removed))
welcome to clc: http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
clc OT guide : http://benpfaff.org/writings/clc/off-topic.html
 
Reply With Quote
 
 
 
 
Barry Schwarz
Guest
Posts: n/a
 
      04-25-2004
On 24 Apr 2004 17:03:01 -0700, (E-Mail Removed) (Matthew Alton) wrote:

>Why does this code generate a SIGSEGV?
>
>#include <stdio.h>
>
>int
>main(void)
>{
> char *s = "ABC";
>
> s[1] = 'X';
>
> return(0);
>}
>
>while this does not:
>
>#include <stdio.h>
>
>int
>main(void)
>{
> char s[] = "ABC";
>
> s[1] = 'X';
>
> return(0);
>}
>


Because your compiler is doing it best to comply with the requirement
that a string literal be non-modifiable. See the faq at
http://www.eskimo.com/~scs/C-faq/top.html. The answer to your
question is near the beginning but you should read the whole thing.


<<Remove the del for email>>
 
Reply With Quote
 
 
 
Reply

Thread Tools

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
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
question row filter (more of sql query question) =?Utf-8?B?YW5kcmV3MDA3?= ASP .Net 2 10-06-2005 01:07 PM
Quick Question - Newby Question =?Utf-8?B?UnlhbiBTbWl0aA==?= ASP .Net 4 02-16-2005 11:59 AM
Question on Transcender Question :-) eddiec MCSE 6 05-20-2004 06:59 AM
Question re: features of the 831 router (also a 924 question) Wayne Cisco 0 03-02-2004 07:57 PM
Syntax Question - Novice Question sean ASP .Net 1 10-20-2003 12:18 PM



Advertisments