LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 02-27-2006, 03:47 PM   #1
rose_bud4201
Member
 
Registered: Aug 2002
Location: St Louis, MO
Distribution: Xubuntu, RHEL, Solaris 10
Posts: 927

Rep: Reputation: 30
ANSI C trim function segfault every time


I'm trying to write a trim function which'll work on c-style strings (google is being astoundingly reticent, probably due to difficulties searching for "C" >_>).

This is what I've got so far:
Code:
#include <stdio.h>

char* trim(char* b)
{
    char *out;
    out = b;
    printf("in trim, out: %s\n", out);
    int len;
    len = strlen(out);
    int i;

    for (i=len-1; i >= 0; i--) {
        if(out[i] != ' ') break;
    }

    printf("i: %d\n", i);
    out[i+1] = '\0';
    return out;
}

int main() {
    char *orig = "    spaces! yaye    ";
    printf("orig: .%s.\n", orig);
    char *changed;
    changed = trim(orig);
    printf("changed: .%s.\n", changed);

    return 0;
}
If I comment out the "out[i+1] = '\0';" line it works, but obviously I don't get the modified string. If I leave it uncommented, segfault. I've actually tried several different implementations of a trim function, and they all invariably segfault.

Can anyone either help me find an alternative way to do this, or give me some pointers towards what I'm doing wrong?

(Note - this only simulates rtrim() at the moment - I'll work on ltrim once this part gets working...).

Thanks!
 
Old 02-27-2006, 03:59 PM   #2
perfect_circle
Senior Member
 
Registered: Oct 2004
Location: Athens, Greece
Distribution: Slackware, arch
Posts: 1,783

Rep: Reputation: 52
you are not aloud to modify this kind of string in ANSI C:
Code:
char *orig = "    spaces! yaye    ";
READ HERE:
http://c-faq.com/decl/strlitinit.html

Last edited by perfect_circle; 02-27-2006 at 04:02 PM.
 
Old 02-27-2006, 04:26 PM   #3
rose_bud4201
Member
 
Registered: Aug 2002
Location: St Louis, MO
Distribution: Xubuntu, RHEL, Solaris 10
Posts: 927

Original Poster
Rep: Reputation: 30
....Hmm. I read what you said, puzzled a bit, went back, and changed my code to char orig[25] = " spaces! yaye ";, and it worked like a charm.

Clearly I'm in need of more of a C refresher course than I thought! I thought that a char array was just a char pointer as far as C was concerned?
 
Old 02-27-2006, 05:13 PM   #4
perfect_circle
Senior Member
 
Registered: Oct 2004
Location: Athens, Greece
Distribution: Slackware, arch
Posts: 1,783

Rep: Reputation: 52
it's something like that but the compiler has every right to store it in the .text region in the assembly code, and the .text region (the region where the instructions are and not the data) is a read-only region.

If I remember correct in C changing this kind of string will cause an "undefined" result. In another compiler this could possibly work but this is not correct.

Also, instead of checking for spaces, you should probably use the isblank or isspace function, to include the tab characters which are different from spaces and should also probably be removed from the end of the line.

Last edited by perfect_circle; 02-27-2006 at 05:17 PM.
 
Old 02-27-2006, 05:16 PM   #5
rose_bud4201
Member
 
Registered: Aug 2002
Location: St Louis, MO
Distribution: Xubuntu, RHEL, Solaris 10
Posts: 927

Original Poster
Rep: Reputation: 30
Wow. That's neat, I definitely learned something today!

Thanks very much, I appreciate the help!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How To Trim A File fpfernando Programming 11 01-05-2006 09:04 AM
trim a line schneidz Programming 2 10-07-2005 07:47 PM
setTimeObject CallBack time function branens Programming 0 02-16-2005 05:30 AM
Cannot find strnset() in string.h: none ANSI-C function xxfunkxx Programming 2 12-09-2004 10:18 AM
How to Trim mpeg video's ? qwijibow Linux - Software 1 01-06-2004 09:28 PM


All times are GMT -5. The time now is 08:20 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration