Christian
Christian

Reputation: 1

Why is this code getting a segmentation fault?

I am trying to write a function that deletes a char c from a string src, and I am getting a seg fault when I try to run it. Here is the function.

void removeChar(char *src, char c){
  int i, j = 0;
  int size;
  char ch1;
  char str1[100];

  size = strlen(src);

  for (i = 0; i < size; i++){
    if (src[i] != c){
      ch1 = src[i];
      str1[j] = ch1;
      j++;
    }
  }
  str1[j] = '\0';
  src = str1;
}

And here is the main function where I am calling it.

int main(int argc, char **argv){
    char *str = "Hello, world!\0";
    printf("%s\n", removeChar(str, 'l'));
}

Upvotes: -1

Views: 122

Answers (5)

Vlad from Moscow
Vlad from Moscow

Reputation: 310910

You assigned pointer src by the address of the first element of a local array

  src = str1;

that will be destroyed after exiting the function. Moreover variable src is a local variable of the function so any changes of it do not influence the original pointer str.

Take into account that you may not change string literals. Any attempt to change a string literal results in undefined behaviour of the program.

Also the function has return type void and may not be used as an outputed object in function printf. Type void is an incomplete type. It has no values.

And there is no need to append explicitly terminating zero to a string literal as you did.

"Hello, world!\0"

String literals already have terminating zeroes. So you could write simply

"Hello, world!"

As I already answered this question then you can visit my personal forum where there is a realization of the corresponding valid function.

If to declare correctly the function like

char * removeChar( char *s, char c );

then the main will look the following way

int main(int argc, char **argv)
{
    char str[] = "Hello, world!";
    printf( "%s\n", removeChar( str, 'l' ) );
}

Upvotes: 2

Lundin
Lundin

Reputation: 213276

You have several bugs:

  1. char *str = "Hello, world!\0";. Setting a non-constant pointer to point at a string literal is always wrong. Instead, declare the variable as const char *str. See this FAQ.

  2. removeChar doesn't return anything so you can't pass it as a parameter to be printed by printf. Your compiler really should have complained here. Chances are that your compiler is misconfigured or you you aren't using it with all warnings enabled.

  3. char str1[100]; You cannot use local variables and then try to pass the contents on to the caller. See this FAQ.

  4. src = str1; doesn't do a thing, since src is only a local copy of the original pointer. With this assignment, you will not change the address of str in main. Which would have been a bug anyway, because of 3) above. You should rewrite your program so that is only uses src and no temporary array.

Upvotes: 1

Shakil Ahamed
Shakil Ahamed

Reputation: 577

Not have enough reputation to comment. So, I had to write this on answer:

As Vlad from Moscow pointed out,

`a local array do not exist after the function terminate`

I suggest you obey the same principle as of standard library functions. If you didn't already notice,none string.h function allocate memory for the user. You must allocate before call.

char *str = "Hello, world!\0";

The above code do not guarantee a modifiable memory. The compiler can set them in read only memory. You should use a array instead.

Upvotes: 0

Rizier123
Rizier123

Reputation: 59681

You can print the string in the function itself! Then it works:

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

void removeChar(char src[], char c){
  int i, j = 0;
  int size;
  char ch1;
  char str1[100];

  size = strlen(src);

  for (i = 0; i < size; i++) {
    if (src[i] != c) {
      ch1 = src[i];
      str1[j] = ch1;
      j++;
    }
  }
  str1[j] = '\0';
  src = str1;

  printf("%s\n", src);

}

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

    char str[] = "Hello, world!";
    removeChar(str, 'l');

    return 0;

}

Upvotes: 1

smali
smali

Reputation: 4805

the return type of this function removeChar(str, 'l') is void not an char array and you are passing this to

printf("%s\n", removeChar(str, 'l'));

so here %s may give you the segmentation fault.

Upvotes: 2

Related Questions