1

I am trying to write a small function to trim left spaces from a string, but I cannot get it right. In this version, I get the following error:

bus error: 10

Could anyone please explain to me what I am doing wrong? I am not looking so much for an alternative piece of code, but would like to understand the errors in my code:

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


void trim_string(char *);

int main(int argc, char *argv[]) {

    char *temp = "   I struggle with strings in C.\n";
    trim_string(temp);
    printf("%s", temp);

    return 0;
}


void trim_string(char *string) {
    char *string_trimmed = "";
    int i=0, j=0;

    while (isblank(string[i])) {
        i++;
    }
    while (string[i] != '\0') {
        string_trimmed[j] = string[i];
        i++;
        j++;
    }
    string_trimmed[j] = '\0';
    strcpy(string, string_trimmed);
}

I have now found a workaround solution, shown below. But I am still not very clear about what I did wrong in the first place:

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

#define MAX_LENGTH 100

void trim_string(char [MAX_LENGTH]);

int main(int argc, char *argv[]) {

    char temp[MAX_LENGTH] = "   I struggle with strings in C.\n";
    trim_string(temp);
    printf("%s", temp);

    return 0;
}


void trim_string(char string[MAX_LENGTH]) {
    char string_trimmed[MAX_LENGTH];
    int i=0, j=0;

    while (isblank(string[i])) {
        i++;
    }
    while (string[i] != '\0') {
        string_trimmed[j] = string[i];
        i++;
        j++;
    }
    string_trimmed[j] = '\0';
    printf("c\n");
    strcpy(string, string_trimmed);
}
Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
lcazarre
  • 713
  • 3
  • 9
  • 15
  • 1
    Had you paid attention to compiler warnings, would you have discovered half of your problem. `char *string_trimmed = "";` should have been `const char *string_trimmed = "";`. But that's not enough - your array is too short. –  Dec 02 '13 at 22:41
  • I compiled with the following settings and did not get any warning: gcc -pedantic -Wall -std=c99 trim.c -o trim Which settings do you suggest? – lcazarre Dec 02 '13 at 22:49
  • I always amazed that why I am not getting this warning! – haccks Dec 02 '13 at 22:55
  • @ShafikYaghmour [Well, I do](http://ideone.com/FSz7W1), not even a warning, but an error. Same with `clang` (some versions, anyway). –  Dec 02 '13 at 23:01
  • @H2CO3 Full program [no warnings](http://coliru.stacked-crooked.com/a/b6b059cf178d053b) your warning is for an unused variable. – Shafik Yaghmour Dec 02 '13 at 23:06
  • @ShafikYaghmour Fair enough, that's for a unused variable. But maybe because it stopped due to the first warning (which is treated as an error). Whatever. It really should be a warning, and string literals aren't to be modified. –  Dec 02 '13 at 23:15
  • @ShafikYaghmour [Here you are](http://stackoverflow.com/questions/59670/how-to-get-rid-of-deprecated-conversion-from-string-constant-to-char-warnin), IDK why this was taken out from `-Wall`, it's a really nasty error. –  Dec 02 '13 at 23:21

1 Answers1

1

Both string and string_trimmed point to string literals, here in main:

char *temp = "   I struggle with strings in C.\n";
             ^
             |
             This is a string literal

temp points to a string literal and the standard says you are not allowed to modify them.

In the function trim_string you are modifying a them which is undefined behavior of which a bus error is one possible result, although anything can happen.

string_trimmed either needs to be an array like this:

char string_trimmed[n] ;

where n is the size of your input using strlen(string) would probably make sense or dynamically allocated via malloc which you would need to free at the end of your function. The same things goes for your input from main, this would work as a substitute:

char temp[] = "   I struggle with strings in C.\n";

For completeness sake, the draft C99 standard section 6.4.5 String literals paragraph 6 says (emphasis mine):

It is unspecified whether these arrays are distinct provided their elements have the appropriate values. If the program attempts to modify such an array, the behavior is undefined.

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740