Andrew Ricci
Andrew Ricci

Reputation: 477

strcat is giving me a segmentation fault error

After verifying that strcat is where the error occurs, I then check the previous example in my assignment. In my previous examples I use strcat(actually strncat) in the same fashion as I do for my following code. I am not too sure.

The purpose of my program is to loop through "string" and remove any occurances the character 'c' from string.

main.c:

 char string[100]={0}, c[3];

 printf("Enter a String: ");
 fgets(string, 100, stdin);
 if (string[98] == '\n' && string[99] == '\0') { while ( (ch = fgetc(stdin)) != EOF && ch != '\n'); }

 printf("Enter a Char: ");
 fgets(c, 2, stdin);
 while ( (ch = fgetc(stdin)) != EOF && ch != '\n');

 rmchr(string, c[0]); 

header:

rmchr(char *string, char c)
{ 
  int i=0;
  char *word[100];

  int s = strlen(string);

  for(i=0; i<=(s-2); i++)
  {
    if(string[i] != c)
    {
      strcat(word, string[i]);
    }
  }  
}

Upvotes: 0

Views: 172

Answers (3)

Lukas
Lukas

Reputation: 3433

The following seems to compile fine but your code doesnt do quite what you said you wanted, "The purpose of my program is to loop through "string" and remove any occurances the character 'c' from string.". the function doesn't remove the character or return a copy of the string with the character excluded. I wrote a function that copies the string after removing the character and returns pointer to it. below is your code a bit modified and under it is my function

//Just a compilable version of your code, not sure if it does what u want
#include <stdio.h>
#include <string.h>

void rmchr(char *string, char c)   
{ 
    int i=0;
    char word[100];

    int s = (int)strlen(string);

    for(i=0; i<=(s-2); i++)
    {
        if(string[i] != c)
        {
            strcat(word, (char *)(&string[i]));
        }
    }  
}

int main(int argc, const char * argv[]) {
    char string[100] = {0}, c[3];
    char ch;

    printf("Enter a String: ");
    fgets(string, 100, stdin);
    if (string[98] == '\n' && string[99] == '\0') { 
       while ( (ch = fgetc(stdin)) != EOF && ch != '\n'); 
    }

    printf("Enter a Char: ");
    fgets(c, 2, stdin);
    while ( (ch = fgetc(stdin)) != EOF && ch != '\n');

    rmchr(string, c[0]); 

    return 0;
}

There you go, with a demo main

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

char* rmchr(char *string, char ch)
{ 
    int counter = 0;
    int new_size = 0;
    char corrected_string[100];

    while (string[counter] != '\n' && string[counter] != '\0' && string[counter] != EOF)  {
        if (string[counter] != ch) {
            corrected_string[new_size] = string[counter];
            new_size++;
        }
        counter++;
    }
    char *new_string = (char *)malloc((new_size+1) * sizeof(char));

    for (int j = 0; j <= new_size; j++) {
        new_string[j] = corrected_string[j];
    }
    return new_string;
}

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

    char *s = "The char 'c' will be removed";
    char *new = rmchr(s, 'c');
    printf("%s", new);
    return 0;
}

Upvotes: 0

Sourav Ghosh
Sourav Ghosh

Reputation: 134356

There are more than one point to mention here, like

  1. rmchr() definition should have a return type, maybe void if you're not returning anything.

    [FWIW, In that case, I wounder, how you'll make use of the local variable word]

  2. inside rmchr(), word needs to be an array of chars, not char pointers. You need to change char * word[100] to char word[100].

  3. In strcat(), both the arguments, needs to be a pointer. You need to use &string[i], in that case.

Upvotes: 0

ouah
ouah

Reputation: 145899

char *word[100];

It will hold a string in your program so use:

char word[100];

that is, an array of char instead of an array of char *.

Then strcat concatenates to a string but word is not initialized. Make it a string with:

word[0] = '\0';

Then string[i] is a character but strcat needs pointers to character arguments: to use a pointer use &string[i].

Finally the problem in your rmchr function is it has to return something, either through the arguments or via a return statement but it doesn't.

Upvotes: 2

Related Questions