473,320 Members | 1,883 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,320 software developers and data experts.

what's reason to have a segmentation error?

What's wrong with the following sudo-code?

******************************
# include <stdio.h>
# include <string.h>

char *test="I am the string you testing on";

int main(){
char *point;
point=test;
while(*point !='\0'){
printf("%c\n",*point);
point++;
if(*point=='s')
*point='\0';
}
}

*****************************
I want to step out of the while loop once I met 's'.
However, when I got to *point='\0';
I have a segmentation error. I know there are better ways. I am just
curious about the reason.
Thanks

Feb 27 '06 #1
9 1383
"questions?" <un************@hotmail.com> writes:
What's wrong with the following sudo-code?

******************************
# include <stdio.h>
# include <string.h>

char *test="I am the string you testing on";

int main(){
char *point;
point=test;
while(*point !='\0'){
printf("%c\n",*point);
point++;
if(*point=='s')
*point='\0';
}
}

*****************************
I want to step out of the while loop once I met 's'.
However, when I got to *point='\0';
I have a segmentation error. I know there are better ways. I am just
curious about the reason.


<http://www.c-faq.com/>, question 1.32.

Also:

"int main()" is acceptable, but "int main(void)" is preferred.

Falling off the end of main without returning a value is acceptable in
C99, but it's still poor style; add a "return 0;".

By "sudo-code", I presume you mean "pseudo-code", but what you posted
is actual C code (other that the mentioned errors), not pseudo-code.
Pseudo-code would probably look more like this:

for each character in the string
if the character is 's' replace it with '\0'

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Feb 27 '06 #2
questions? wrote:

What's wrong with the following sudo-code?

******************************
# include <stdio.h>
# include <string.h>

char *test="I am the string you testing on";

int main(){
char *point;
point=test;
while(*point !='\0'){
printf("%c\n",*point);
point++;
if(*point=='s')
*point='\0';
}
}

*****************************
I want to step out of the while loop once I met 's'.
However, when I got to *point='\0';
I have a segmentation error. I know there are better ways. I am just
curious about the reason.


test is a pointer to the first character of a non-modifiable
string. If you wanted it to be a modifiable array you would have
written:

char test[] = "I am the string you testing on";

and then your code would have worked.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>

Feb 27 '06 #3

CBFalconer wrote:
questions? wrote:

What's wrong with the following sudo-code?

******************************
# include <stdio.h>
# include <string.h>

char *test="I am the string you testing on";

int main(){
char *point;
point=test;
while(*point !='\0'){
printf("%c\n",*point);
point++;
if(*point=='s')
*point='\0';
}
}

*****************************
I want to step out of the while loop once I met 's'.
However, when I got to *point='\0';
I have a segmentation error. I know there are better ways. I am just
curious about the reason.


test is a pointer to the first character of a non-modifiable
string. If you wanted it to be a modifiable array you would have
written:

char test[] = "I am the string you testing on";

and then your code would have worked.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>


Thanks you guys for pointing out thing in my style of writing code.
I got it. that's cool

Feb 27 '06 #4
char *test="I am the string you testing on";

this code means
string("I am the string you testing on") is placed in data area. data
area means all constant data region.
so, test point to some place of data area.

char test[]="I am the string you testing on";
in this case,
also string("I am the string you testing on") is placed in data area.
but test has test's array. and it filled as string("I am the string you
testing on").
so, you can replace charactor of test's array.

Feb 27 '06 #5
"se*****@gmail.com" <se*****@gmail.com> writes:
char *test="I am the string you testing on";

this code means
string("I am the string you testing on") is placed in data area. data
area means all constant data region.
so, test point to some place of data area.


There's actually nothing in C called the "data area". Also, there's
no requirement that string literals are stored as constants.
Attempting to modify a string literal invokes undefined behavior
(which means it *can* work, but you can't depend on it).

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Feb 27 '06 #6
questions? wrote:
What's wrong with the following sudo-code? correction its not pseudo code :)
char *test="I am the string you testing on";

int main(){
char *point;
point=test;
while(*point !='\0'){
printf("%c\n",*point);
point++;
if(*point=='s')
*point='\0';
}
} I want to step out of the while loop once I met 's'.
However, when I got to *point='\0';
I have a segmentation error. I know there are better ways. I am just
curious about the reason.
It is pretty simple why you are getting a seg fault. All strings are by
default const char * .This means if try changing it you will be
sefgaulted. I hope you understand this :)

Thanks

you are welcome :)

Feb 27 '06 #7
pradeep singh wrote:
questions? wrote:
What's wrong with the following sudo-code?

correction its not pseudo code :)
char *test="I am the string you testing on";

int main(){
char *point;
point=test;
while(*point !='\0'){
printf("%c\n",*point);
point++;
if(*point=='s')
*point='\0';
}
}

I want to step out of the while loop once I met 's'.
However, when I got to *point='\0';
I have a segmentation error. I know there are better ways. I am just
curious about the reason.


It is pretty simple why you are getting a seg fault. All strings are by
default const char * .


no. this is wrong. Strings are not const char*. The standard merely
says
it is undefined behaviour to modify the value of a string literal. This
allows
implementors to place the string in read-only memory
This means if try changing it you will be
sefgaulted. I hope you understand this :)


I hope he *didn't* understand it as it was wrong :-)
--
Nick Keighley

Feb 27 '06 #8
"pradeep singh" <pr*************@gmail.com> writes:
questions? wrote:
What's wrong with the following sudo-code? correction its not pseudo code :)
char *test="I am the string you testing on";

[...] It is pretty simple why you are getting a seg fault. All strings are by
default const char * .This means if try changing it you will be
sefgaulted. I hope you understand this :)


No string literals are not const char*. A string literal is of type
char[N], where N is the number of characters required (the length plus
one for the trailing '\0'). Like any expression of array type, it's
converted to a pointer value in most contexts, in this case a pointer
value of type char* (no const).

Attempting to modify a string literal invokes undefined behavior
because the standard says so, not because it's const.

(Making string literals const might have been better in some ways,
<OT>and C++ does so</OT>, but it would also have broken existing
code.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Feb 27 '06 #9
On 27 Feb 2006 00:33:14 -0800, "Nick Keighley"
<ni******************@hotmail.com> wrote:
pradeep singh wrote:

It is pretty simple why you are getting a seg fault. All strings are by
default const char * .


no. this is wrong. Strings are not const char*. <snip>

This means if try changing it you will be
sefgaulted. I hope you understand this :)


I hope he *didn't* understand it as it was wrong :-)


It's OK if he understood it as long as he didn't believe it. :-))

- David.Thompson1 at worldnet.att.net
Mar 6 '06 #10

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

Similar topics

1
by: Justin Tang | last post by:
Hi I am wondering about the segmentation fault in PHP. Namely, I'm running PHP version 4.6.9 on my server right now and when I try to process a large piece of text via textarea(3k+), the resulting...
3
by: diyanat | last post by:
i am writing a cgi script in C using the CGIC library, the script fails to run, i am using apache on linux error report from apache : internal server error Premature end of script headers:...
18
by: Matt | last post by:
Hi, I have a probelm here: If I declare: char *p="Hello";
1
by: Dawn Minnis | last post by:
Hey guys - this code when called with parameters: driver.o n n 12 12 12 12 12 12 2.6 3.2 is kicking back a segmentation fault. I've read the rest of the postings but am still confused. Can...
3
by: I_have_nothing | last post by:
Hi! I am new in C. I got a lots of "Segmentation Fault"s in my code. I guess One possibility is: if " int array_i; " is declard and the code trys to access "array_i", a Segmentation Fault will...
0
by: justarrived | last post by:
Hi, I am working on a Pro-c program on Unix. It compiles/builds properly but while trying to run it I am receiving segmentation violation at- sqlcxt((void **)0, &sqlctx, &sqlstm, &sqlfpn); The...
3
by: madunix | last post by:
My Server is suffering bad lag (High Utlization) I am running on that server Oracle10g with apache_1.3.35/ php-4.4.2 Web visitors retrieve data from the web by php calls through oci cobnnection...
3
by: Dhieraj | last post by:
While compiling a C++ code I am getting the following error : CC -c -I/opt/iona/artix/2.0/include -I/opt/iona/asp/6.0/include -I/opt/ar/api63/include -I//var/tmp/vidya/aotscommon/include ...
2
by: Nagaraj | last post by:
Hi all, I am new to Linux platform. I am writing some C programs on Linux platform. It gives the segmentation fault error, what is segmentation fault and how to remove it. please reply.
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.