0

I am trying to get 3 characters combinations using recursion but after some calls i get Segmentation fault

#include <cs50.h>
#include <stdio.h>

void three_Characters(char c, char c2, char c3);

int main(void){
    three_Characters('A', 'A', 'A');
    return 0;
}

void three_Characters(char c, char c2, char c3){

//print 3-characters 
    printf("%c%c%c - ", c, c2, c3);

    /*Recursion termination*/
    if(c == 'z' && c2 == 'z' && c3 == 'z'){
        return;
    }

    /*Avoid symbol characters */
    if(c3 == 'Z'){
        c3 += 6;
        if(c2 == 'Z'){
            c2 += 7;
            if(c == 'Z'){
            c += 7;
            }
        }
    }

    if(c3 == 'z'){
        if(c2 == 'z'){
            c += 1;   c2 = 65;   c3 = 64;
        }else{
            c2 += 1;  c3 = 64;
        }
    }
    three_Characters(c, c2, c3 + 1); 
}
1

If I had to bet on something it would be for a stack overflow, although I recognize that I am not a great expert in terms of the stack, it is bad programming practice to use large amounts of memory in the stack, one solution could be to use the dynamic allocation of memory, although I do not see very clear how to use it with recursion

0

Answer from stack overflow illustrating the reason of segmentation fault and the recursion solution may take more time than loops

https://stackoverflow.com/questions/52103830/program-received-signal-sigsegv-segmentation-fault-when-trying-to-get-all-ke?noredirect=1#comment91179536_52103830

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .