1
votes

Im getting the error "Segmentation fault (core dumped)" when I run this program. I am new to c programming so its probably something stupid but i cant figure it out.

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

void swap(char *x, char *y){
    char temp = *x;
    *x=*y;
    *y=temp;
}

char** getPermutations(char *string){
    int length = strlen(string);
    int startIndex = 0;
    int endIndex = length - 1;

    if(startIndex == endIndex){
            printf("%s\n", string);
    }else{
            for(int j = startIndex; j<=endIndex; j++){
                    swap((string + startIndex),(string + j));
                    getPermutations(string);
                    swap((string+startIndex),(string+j));
            }  
    }    
}

int main(int argc, char *argv[]){
     if(argc>2){
            printf("\nToo Many arguments\n");
            exit(0);
    }else{
            printf("%s",argv[1]);
            char * str = malloc(strlen(argv[1]) + 1);
            strcpy(str,argv[1]);
            getPermutations(str);
    }
}
2
don't forget to free the allocated data at the end using free() - Cherubim
when compiling, always enable all the warnings, then fix those warnings that are output by the compiler. (for gcc, at a minimum use: -Wall -Wextra -pedantic I also use: -Wconversion std=gnu99` ) - user3629249
for ease of readability and understanding: 1) separate code blocks (for, if, else, while, do...while, switch, case, default) via a blank line 2) consistently indent the code. never use tabs for indenting as every wordprocessor/editor has the tab stops/tab width set for personal preference. Suggest using 4 spaces for each indent level as that is wide enough to be visible even with variable width fonts and still allows for many indent levels across the page - user3629249
error messages 'should' be sent to stderr, not stdout so when writing an error message, use: fprintf( stderr, ........); rather than printf( ....... ); When finding a problem with the command line arguments, output a usage statement, similar to: fprintf( stderr, "USAGE: %s <argument>", argv[0] ); The current output does not tell the user anything about what the command line should be. Never access beyond argv[0] without first checking argc to assure that such argument actually exists - user3629249
when calling most system functions, for instance malloc(), always check the returned value to assure the function was successful. - user3629249

2 Answers

2
votes

Your issue is that getPermutations calls itself endlessly. You need to pass something extra to it so it can know when to stop. As is, it just calls itself over and over until you have a stack overflow.

Also, you have getPermutations setup to return a char**, but then you never return anything. So that's odd too.

1
votes

My suggestions:

  1. Change the return type of the function to void since it does not return anything.

  2. Change the name of the function to printPermutations since it just prints the permutations.

  3. Provide a way to end the recursion. Pass startIndex as an argument.

  4. Pass the length of the string so you don't compute it every time the function is called recursively.

  5. Change the comparison operator to >= to account for zero length strings.


void printPermutations(char *string, int startIndex, int length){
   int endIndex = length - 1;

   if(startIndex >= endIndex){
      printf("%s\n", string);
   }else{
      for(int j = startIndex; j<=endIndex; j++){
         swap((string + startIndex),(string + j));
         printPermutations(string, startIndex+1, length);
         swap((string+startIndex),(string+j));
      }  
   }    
}

and call it with:

printPermutations(str, 0, strlen(str));