AL-zami
AL-zami

Reputation: 9066

Permutation of string

I am trying to implement a code where i have to permute a string.Whenever i try to execute my code i get the following errors and warnings;

1.passing argument 1 of permute makes integer from pointer without a cast

2.expected char but argument is of type char *

3.conflicting type for permute

what might cause those errors in my program?

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

void swap(char *first,char *second);
void permute(char a,int l,int r);

int main(){
   char str[] = "ABC";
   int size = strlen(str);
   permute(str,0,size-1);
   return 0;
}
void permute(char *a,int l,int r){
   if (l==r){
       printf("%s\n",a);
   }else{
       int i;
       for(i=l;i<=r;i++){
           swap((a+l),(a+i));
           permute(a,l+1,r);
           swap((a+l),(a+i));
       }
   }

}
void swap(char *first,char *second){
    char *temp;
    *temp = *first;
    *first = *second;
    *second = *temp;
}

Upvotes: 0

Views: 159

Answers (2)

Ethan Williams
Ethan Williams

Reputation: 88

Your error is that your data type is char for function permute where you want it to be an array. You want to use the parameter char *a[]. Permute should be

void permute(char *a[], int l, int r)

Upvotes: 0

dbush
dbush

Reputation: 223699

There's an error in your function prototype. It's declared like this:

void permute(char a,int l,int r);

But defined like this:

void permute(char *a,int l,int r) {

Note that the type of the first argument does not match. You need to change the prototype to match the definition.

Unrelated to that, your swap function is using a pointer temp that is being dereferenced without being set. This is undefined behavior and will likely cause a core dump.

Since you're swapping characters here, you only need a char, not a char *.

void swap(char *first,char *second){
    char temp;
    temp = *first;
    *first = *second;
    *second = temp;
}

Upvotes: 3

Related Questions