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);
}
}
free()- Cherubimgcc, at a minimum use:-Wall -Wextra -pedanticI also use:-Wconversionstd=gnu99` ) - user3629249stderr, notstdoutso when writing an error message, use:fprintf( stderr, ........);rather thanprintf( ....... );When finding a problem with the command line arguments, output ausagestatement, 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 beyondargv[0]without first checkingargcto assure that such argument actually exists - user3629249malloc(), always check the returned value to assure the function was successful. - user3629249