what's reason to have a segmentation error?

Q

questions?

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
 
K

Keith Thompson

questions? said:
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'
 
C

CBFalconer

questions? said:
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/>
 
Q

questions?

CBFalconer said:
questions? said:
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
 
S

semiels

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.
 
K

Keith Thompson

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).
 
P

pradeep singh

questions? said:
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 :)
you are welcome :)
 
N

Nick Keighley

pradeep said:
questions? wrote:
correction its not pseudo code :)



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 :)
 
K

Keith Thompson

pradeep singh said:
questions? said:
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.)
 
D

Dave Thompson

pradeep singh wrote:

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

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top