Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > segmentation fault

Reply
Thread Tools

segmentation fault

 
 
dough
Guest
Posts: n/a
 
      10-03-2005
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"));

 
Reply With Quote
 
 
 
 
Gordon Burditt
Guest
Posts: n/a
 
      10-03-2005
>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
 
Reply With Quote
 
 
 
 
A. Sinan Unur
Guest
Posts: n/a
 
      10-03-2005
"dough" <(E-Mail Removed)> 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 <(E-Mail Removed)>
(reverse each component and remove .invalid for email address)
 
Reply With Quote
 
dough
Guest
Posts: n/a
 
      10-03-2005
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?

 
Reply With Quote
 
James McIninch
Guest
Posts: n/a
 
      10-03-2005
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
 
Reply With Quote
 
uououo
Guest
Posts: n/a
 
      10-03-2005
#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;
}

 
Reply With Quote
 
Artie Gold
Guest
Posts: n/a
 
      10-03-2005
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!"
 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      10-03-2005
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"));
>

 
Reply With Quote
 
uououo
Guest
Posts: n/a
 
      10-03-2005
you're right

 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      10-03-2005
"dough" <(E-Mail Removed)> 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 <(E-Mail Removed)>
(reverse each component and remove .invalid for email address)
 
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
Segmentation fault using Firefox 15.0.2 Keith Lee Firefox 3 04-29-2006 05:45 PM
Xerces on Solaris - Segmentation fault ldvmbs@gmail.com XML 0 05-16-2005 07:21 AM
Xerces XML Parser Segmentation fault with Java Pud XML 0 11-06-2003 05:07 PM
Intel Xeon + Linux + IBM sdk 1.3.1 - getting Segmentation fault Alex Hunsley Java 17 11-06-2003 12:12 AM
Re: segmentation fault exception handling Ivan Vecerina C++ 0 06-29-2003 10:56 PM



Advertisments