0

I am making a program in C which generates all the combinations of the given input.
For example,if input is ABCD,then it will give me an array of strings which will be {"A","B","C","D","AB","BC","CD","ABC","BCD","ABCD"} without scrambling the word.
Here is my code:

char **word_generator(char *word)
{
    char *combinations[500];
    int index = 0;
    int val = 0;
    int index1  = 0;
    int len = strlen(word);
    for (int i = 0;i < pow(len,2);i++)
    {
        for (int j = 0;j < len - index;j++)
        {
            val = 0;
            for (int k = j;k < j+index+1;k++)
            {
                combinations[index1][val] = word[k];
                printf("%c ",word[k]);
                val += 1;
            }
            printf("\n");
            if (val == len)
            {
                return combinations;
            }
            index1 += 1;
        }
        index += 1;
    }
}

I have tested the code and if gives perfect results if I don't bother about array and return value and just print the output.
But there are two problems in it.

  1. It gives the error function returning address of a local variable.
  2. Some problem is in the line combinaitons[index1][val] = word[k]; because I have tested the code and everything goes fine before that line.
Michael Stevens
  • 220
  • 1
  • 10
  • 1
    Have you learned about the difference between the stack and the heap? A variable allocated with malloc() goes on the heap, and persists after the function returns. A variable declared like char foo[10]; goes on the stack and disappears. – Dave S Jul 04 '21 at 16:31
  • Please provide a [mre] which demonstrates your problem. – Yunnosch Jul 04 '21 at 16:35
  • 2
    `combinations` is an array of pointers, but you've never allocated memory for them to point to. Also, you can't return a local array, see https://stackoverflow.com/questions/11656532/returning-an-array-using-c – Barmar Jul 04 '21 at 16:37
  • 2
    _Side note:_ `int len = strlen(word); for (int i = 0; i < pow(len,2); i++)` is _very_ slow. The `pow` function is slow [and can be replaced with `len * len`]. And, it is called on each loop iteration instead of just once. You can replace with: `int len = strlen(word); int len2 = len * len; for (int i = 0; i < len2; i++)` – Craig Estey Jul 04 '21 at 16:39
  • Hmm, I'd also expect `""` in the output set. Aditya76, what output set should result from `word_generator("")`? – chux - Reinstate Monica Jul 04 '21 at 21:40

1 Answers1

0
            combinations[index1][val] = word[k];

But all elements of combinations are clearly uninitalized. Anything can happen.

Joshua
  • 40,822
  • 8
  • 72
  • 132
  • Then how can I initialize all elements of this array because I don't know the size of it because user can give any word so it's size may vary – Michael Stevens Jul 05 '21 at 04:03
  • @Aditya76: Have you reached `malloc` yet? But I don't understand your code so I can't adapt it even if you have. – Joshua Jul 05 '21 at 14:43
  • I don't know why you can't understand my code. it's pretty easy to understand if you try to. – Michael Stevens Jul 06 '21 at 17:51
  • @Aditya76: To start off with, essentially no algorithm increments index at two levels of nested loops. – Joshua Jul 06 '21 at 19:01