Home > Blockchain >  What's causing a segmentation fault in my code and what I should do to fix it?
What's causing a segmentation fault in my code and what I should do to fix it?

Time:07-27

I've been getting segmentation fault and I don't know why. I have a feeling that it's caused by the code under if(isPalindrome(str_array[i]) == 0) but I don't know which one and what to do about it.

P.S. I'm still a student so I would appreciate it if the suggestions do not go far from the level of code that I have here.

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

int isPalindrome(char check[]);

int main() {
    int array_size = 5, i;
    char *str_array[array_size], *palindrome_array[array_size], str_length[100];

    for(i = 0; i < array_size; i  ) {
        printf("Enter word #%d: ", i 1);
        scanf("%[^\n]%*c", str_length); //"%[^\n]%*c : pattern matching - allows inputs to have spaces

        str_array[i] = (char*) malloc(sizeof(char) * strlen(str_length));
        strcpy(str_array[i],str_length); 

        if(strcmp(str_array[i],"exit") == 0) {
            break;
        }
    
        if(isPalindrome(str_array[i]) == 0) {
            palindrome_array[i] = (char*) malloc(sizeof(char) * strlen(str_length));
            strcpy(palindrome_array[i], str_length); 
            printf("'%s' is a palindrome \n", palindrome_array[i]);
        } else printf("'%s' is NOT a palindrome \n", str_array[i]);
    }


    //BONUS: prints all the palindrome strings inputted
    printf("Your palindrome words are: \n");
    for(i = 0; i < array_size; i  ) {
        printf("%d.)%s \n", i 1, palindrome_array[i]);
    }

    return 0;
}


int isPalindrome(char check[]) {        // from string to individual characters
    int middle = strlen(check) / 2;     //gets the half of the string's length
    int length = strlen(check);

    for(int i = 0; i < middle; i  ) {
        if(check[i] != check[length - i - 1]) {
            return 1;
        } 
    }
    return 0;
}   

CodePudding user response:

There are two main drawbacks.

To allocate a memory for a string you need to reserve space for the terminating zero character '\0' of the string.

So you need to write for example

str_array[i] = (char*) malloc(sizeof(char) * ( strlen(str_length)   1 ) );

Also the array palindrome_array was not initialized.

char *str_array[array_size], *palindrome_array[array_size], str_length[100];

As not all tested strings are palindromes then some elements of the array still stay uninitialized for some values of the index i due to this if statement

    if(isPalindrome(str_array[i]) == 0) {
        palindrome_array[i] = (char*) malloc(sizeof(char) * strlen(str_length));
        strcpy(palindrome_array[i], str_length); 
        printf("'%s' is a palindrome \n", palindrome_array[i]);
    } else printf("'%s' is NOT a palindrome \n", str_array[i]);

As a result this for loop

for(i = 0; i < array_size; i  ) {
    printf("%d.)%s \n", i 1, palindrome_array[i]);
}

can invoke undefined behavior.

You need to declare a separate index for the array and to use it instead of the index i.

In fact the array str_array is just redundant. It is enough to use these two arrays.

char *palindrome_array[array_size], str_length[100];

The parameter of the function isPalindrome should be declared with the qualifier const

int isPalindrome( const char check[]);

because the passed string is not changed within the function. Also it is much better when the function returns a npn-zero integer when a string is a palindrome and zero otherwise.

In the call of scanf you should specify the maximum length of the input string and the conversion specifier c should be removed. For example

scanf( "            
  • Related