Search code examples
carraysstringpermute

storing the permuted strings into an array


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;
}

Solution

  • 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.