Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   segmentation fault (http://www.velocityreviews.com/forums/t439634-segmentation-fault.html)

dough 10-03-2005 01:00 AM

segmentation fault
 
Can anyone tell me why I get a segmentation fault with the following
code and call?

char *strlwr(char *s)
{
while( s != NULL && *s != '\0' )
{
*s = tolower(*s);
s++;
}

return s;
}

printf("%s", strlwr("HAPPY"));


Gordon Burditt 10-03-2005 01:06 AM

Re: segmentation fault
 
>Can anyone tell me why I get a segmentation fault with the following
>code and call?


Attempting to write on a character in a quoted string constant
invokes the wrath of undefined behavior. The compiler is
permitted to place such data in ROM or memory where write is
not allowed.

>printf("%s", strlwr("HAPPY"));


Gordon L. Burditt

A. Sinan Unur 10-03-2005 01:18 AM

Re: segmentation fault
 
"dough" <vicluo@gmail.com> wrote in news:1128301233.146582.222730
@g14g2000cwa.googlegroups.com:

> Can anyone tell me why I get a segmentation fault with the following
> code and call?
>
> char *strlwr(char *s)
> {
> while( s != NULL && *s != '\0' )
> {
> *s = tolower(*s);
> s++;
> }
>
> return s;
> }
>
> printf("%s", strlwr("HAPPY"));


This is a FAQ:

http://www.eskimo.com/~scs/C-faq/q16.6.html

Also, functions with the str prefix are in reserved by the standard.

Note that the function above will return a pointer to the terminating nul
character.

#include <ctype.h>
#include <stdio.h>

char *mystrlwr(char *s) {
if (s) {
char *t = s;
while (*t) {
*t = tolower((unsigned char) *t);
++t;
}
}
return s;
}

int main(void) {
char s[] = "HELLO";
printf("%s\n", mystrlwr(s));
return 0;
}

D:\Home> gcc -Wall s.c -o s.exe

D:\Home> s
hello

--
A. Sinan Unur <1usa@llenroc.ude.invalid>
(reverse each component and remove .invalid for email address)

dough 10-03-2005 01:46 AM

Re: segmentation fault
 
thank you for that. it cleared a few of my questions.

what happens if i need s to be a pointer because i don't know how big
the string is:

char *s;
while( fscanf(f, "%s", s) != EOF )
{
printf("%s", mystrlwr(s));
}

Is there some kind of malloc() that i need to do as well?


James McIninch 10-03-2005 02:45 AM

Re: segmentation fault
 
It's not the function, it's that you return and address that is past the end
of the string.

dough wrote:

> Can anyone tell me why I get a segmentation fault with the following
> code and call?
>
> char *strlwr(char *s)
> {
> while( s != NULL && *s != '\0' )
> {
> *s = tolower(*s);
> s++;
> }
>
> return s;
> }
>
> printf("%s", strlwr("HAPPY"));


--
Remove '.nospam' from e-mail address to reply by e-mail

uououo 10-03-2005 03:27 AM

Re: segmentation fault
 
#include <stdio.h>
#include <ctype.h>

char *strlwr(char *s)
{
if( s ){
char *t=s;
for(; *t; ++t)
( *t >= 'A' && *t <= 'Z' ? *t-='A'-'a':0 );
}
return s;
}

int main()
{
char array[]="HAPPY123";
printf("%s", strlwr(array));

return 0;
}


Artie Gold 10-03-2005 03:31 AM

Re: segmentation fault
 
James McIninch wrote:
[top posting corrected]
>
> dough wrote:
>>Can anyone tell me why I get a segmentation fault with the following
>>code and call?
>>
>>char *strlwr(char *s)
>>{
>> while( s != NULL && *s != '\0' )
>> {
>> *s = tolower(*s);
>> s++;
>> }
>>
>> return s;
>>}
>>
>>printf("%s", strlwr("HAPPY"));

>
>
> It's not the function, it's that you return and address that is past

the end
> of the string.
>


Erm, no. See elsethread. [It's in the attempt to modify a string literal
-- invoking undefined behavior.] Besides, the returned pointer would be
pointing to the terminating null character of the string. Even
derefencing it would be OK (assuming it *was*, in fact a proper C string).

--ag

--
Artie Gold -- Austin, Texas
http://goldsays.blogspot.com (new post 8/5)
http://www.cafepress.com/goldsays
"If you have nothing to hide, you're not trying!"

Martin Ambuhl 10-03-2005 04:09 AM

Re: segmentation fault
 
dough wrote:
> Can anyone tell me why I get a segmentation fault with the following
> code and call?


Because you are trying to modify a string literal.
(You are also improperly invading the implementation's namespace by
using the identifier 'strlwr' to name your function. Stop it.)

This question and variations on it have been asked so often that
whenever it is asked it signals that the questioner has not bothered to
follow simple usenet etiquette. It is expected that a civilized poster
to usenet will have
a) checked the FAQ before posting,
b) followed the newsgroup before posting,
c) checked the archives before posting.
That you could ask your question shows that you have done *none* of the
above.


>
> char *strlwr(char *s)
> {
> while( s != NULL && *s != '\0' )
> {
> *s = tolower(*s);
> s++;
> }
>
> return s;
> }
>
> printf("%s", strlwr("HAPPY"));
>


uououo 10-03-2005 05:56 AM

Re: segmentation fault
 
you're right :)


A. Sinan Unur 10-03-2005 12:06 PM

Re: segmentation fault
 
"dough" <vicluo@gmail.com> wrote in news:1128304016.781727.253100
@z14g2000cwz.googlegroups.com:

> thank you for that.


Thank who for what? Please quote an appropriate amount of context.

> it cleared a few of my questions.
>
> what happens if i need s to be a pointer because i don't know how big
> the string is:


Are you trying to get the whole program written by others?

> char *s;
> while( fscanf(f, "%s", s) != EOF )
> {
> printf("%s", mystrlwr(s));
> }
>
> Is there some kind of malloc() that i need to do as well?


1. s is pointing to garbage.
2. But more importantly, there is no amount of memory you can allocate
for s that would not be susceptible to being overflowed.

In the case of dealing with whole files, I would be inclined to keep
things simple, and use fgetc.

Sinan

PS: Please read the whole FAQ in its entirety at least once.

--
A. Sinan Unur <1usa@llenroc.ude.invalid>
(reverse each component and remove .invalid for email address)


All times are GMT. The time now is 02:43 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.