0

I have been trying to store the permuted strings from the following function into an array. I am getting the following error,

 Error  2   error C2106: '=' : left operand must be l-value

I want to be able to store all the permuted string and retrieve them one by one.

#include<stdio.h>
#include<string.h>
const char cstr[100][100];
char* permute(const char *a, int i, int n)
{
    int j;
    if (i == n)
    {
         cstr [k] =a;
         k++;

    }

    else 
   {
        for (j = i; j <= n; j++)
       {
            swap((a + i), (a + j));
            permute(a, i + 1, n);
            swap((a + i), (a + j)); //backtrack
       }
  }
  return cstr;
}
int main ()
{
  char str1 [100];
  printf ( "enter a string\n" );
  scanf( "%d" , &str );
  permute ( str1 , 0 , n-1 );
  //can't decide what parameter to consider to terminate the loop
  printf( "%s" , cstr[i] ); /*then print the strings returned from permute 
                         function*/
  return 0;
}
Kiran JD
  • 521
  • 6
  • 15
  • 1
    [mcve], please! And you don't return anything from `permute` which is designed to return a `char*` – Spikatrix Apr 30 '17 at 04:09
  • yes, Now it does – Kiran JD Apr 30 '17 at 04:15
  • 1
    What is `ptr`? How/where is it declared. Unless you have a lot of *globals* declared, your code will not even compile. "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: [**How to create a Minimal, Complete, and Verifiable example**](http://stackoverflow.com/help/mcve)." – David C. Rankin Apr 30 '17 at 04:24
  • 1
    Cannot see the error. Again, [mcve], please! – Spikatrix Apr 30 '17 at 04:25
  • Sorry for that. This is only second question for me – Kiran JD Apr 30 '17 at 04:27
  • 1
    That's why @CoolGuy was gently coaxing you to post the rest of it, so we can attempt to compile it and help you `:)` – David C. Rankin Apr 30 '17 at 04:28
  • Now, 'a' contains the permuted string each time is is called, I want to be able to store them in an array – Kiran JD Apr 30 '17 at 04:28
  • You mean the whole code? @DavidC.Rankin – Kiran JD Apr 30 '17 at 04:29
  • I did not intend to put ptr there. I was trying out something. This is the new edit @CoolGuy – Kiran JD Apr 30 '17 at 04:32
  • 1
    `cstr [k] =a` isn't right. Assuming `cstr` is a pointer to `char`, you're trying to assign a character (`char`) to a pointer to `const char` (`const char *`). Perhaps you wanted `cstr[k] = a[k]`? – Spikatrix Apr 30 '17 at 04:39
  • It gives the same error when I do that @CoolGuy – Kiran JD Apr 30 '17 at 04:48
  • 2
    Before trying to permute strings, you are making fundamental mistakes that calls into question whether you have a command of the basics. `scanf( "%d" , &str );` `str` is not declared anywhere. You convert **integers** with the `%d` *format specifier* -- not strings. Presuming `str` is an character array, it's just `str` in the `scanf` argument list, not `&str` (an array is converted to a pointer when passed as a parameter). You fail to check the **return** of `scanf`. `str1` isn't initialized when passed to `permute`. `k` is never declared or initialized. Start with the basics. – David C. Rankin Apr 30 '17 at 05:51

2 Answers2

0

cstr[k] = a; is where your error is.

cstr[k] is a char[100], but a is a char*. These are fundamentally different and cannot be assigned to each other. You want to do a strcpy(cstr[k], a) instead (or a memcpy).

cstrk[k] refers to a character array of size 100 and arrays cannot directly be assigned to in C, therefore it is not an l-value expression.

Felix Guo
  • 2,700
  • 14
  • 20
0

Following is the fully working program

#include <stdio.h>
#include <string.h> 
char cstr[100][100];
int k;
/* Function to swap values at two pointers */
void swap (char *x, char *y)
{
    char temp;
    temp = *x;
    *x = *y;
    *y = temp;
}
/*  End of swap()  */

/*  Function to print permutations of string  */
char permute(char *a, int i, int n)
{
    int j;
    if (i == n)
    {   
        strcpy(cstr[k],a);
        k++;
       // printf("%s\n", a);
    }
    else {
        for (j = i; j <= n; j++)
        {
            swap((a + i), (a + j));
            permute(a, i + 1, n);
            swap((a + i), (a + j)); //backtrack
        }
    }
    return cstr;
}

/*  The main() begins  */
int main()
{
    char a[20];
    int n,x,i;
    printf("Enter a string: ");
    scanf("%s", a);
    n = strlen(a);
    printf("Permutaions:\n"); 
    permute(a, 0, n - 1);
    for(i=0;i<k;i++)
    printf("%s\n",cstr[i]);
    getchar();
    scanf("%d",&x);
    return 0;
}
Kiran JD
  • 521
  • 6
  • 15