字符串的排列

Permutation of string

我正在尝试实现我必须置换的代码 string.Whenever 我尝试执行我的代码 我收到以下错误和警告;

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

什么可能导致我的程序出现这些错误?

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

你的函数原型有错误。它是这样声明的:

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

但定义如下:

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

注意第一个参数的类型不匹配。您需要更改原型以匹配定义。

与此无关,您的 swap 函数正在使用一个指针 temp,该指针在未设置的情况下被取消引用。这是未定义的行为,可能会导致核心转储。

因为你在这里交换字符,你只需要 char,而不是 char *

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

你的错误是你的数据类型是 char for function permute 而你希望它是一个数组。您要使用参数 char *a[]。排列应该是

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