【问题标题】:Swap between array of strings only by pointers in C仅通过 C 中的指针在字符串数组之间交换
【发布时间】:2015-11-01 12:39:10
【问题描述】:

我需要绘制一个函数,该函数接收一个指针数组(指向字符串)并按字符串长度对指针进行排序。最短的字符串会排在第一位,以此类推。我尝试了以下代码,但它不起作用:

void Q5(){
  char str[MAXL][MAXC];
  int i;
  char** p;
  printf("Please enter %d Strings max length = %d\n",MAXL,MAXC);
  for (i = 0; i < MAXL; i++){
    scanf("%s", str[i]);
  }
  p = &str;
  sort_String(p,MAXL);
  printf("\n");
  for (i = 0; i < MAXL; i++){
    printf("%s", str[i]);
    printf("\n");
  }
}

void sort_String(char* str,int size){
char* tempP=*str;
char* i,*j;
for (i=str; i < str+size;i++){
    for (j=i+1; j < str+size; j++){
        if (strlen(i) > strlen(j)){
            tempP = j;
            j = i;
            i = tempP;
        }
    }
}

【问题讨论】:

  • @Ofir N 你可以使用标准的 C 函数 qsort。:) 至于你的代码,那么它是无效的。您没有指向字符串的指针数组。你有一个二维字符数组。
  • 您不会在此代码中收到任何类型的警告。我看到了其中的问题。

标签: c arrays string sorting pointers


【解决方案1】:

要实现您所说的,您应该动态地malloc 必要的部分,然后您可以使用此代码。但是您不能更改数组位置(位置),这是不允许的。

// L-> number of strings
// C-> character needed

char **str=malloc(sizeof(char*)*L);
for(i=0;i<MAXL;i++)
  str[i]=malloc(sizeof(char)*C);

然后您可以简单地应用您编写的冒泡排序。 您需要在排序函数中传递char**

不需要遍历sort 函数中的指针变量。如果你像我提到的那样动态分配,那么你可以这样编码:-

void sort_String(char** str,int size)
{
char* tempP;
int i,j;
for (i=0; i < size;i++){
    for (j=i+1; j < size; j++){
        if (strlen(str[i]) > strlen(str[j])){
            tempP = str[j];
            str[j ]= str[i];
            str[i] = tempP;
        }
    }
}
}

虽然没有给出正确的代码,但很少有问题,例如 Q5() 的 } 缺失。举个MCV的例子。


注意:我假设您使用的是 c 编译器。这就是我避免选角的原因。在 C 中,您不需要强制转换 malloc 的返回值。 malloc 返回的指向 void 的指针会自动转换为正确的类型。但是,如果您希望您的代码使用 C++ 编译器进行编译,则需要进行强制转换。

【讨论】:

  • @Ofir N:检查答案。希望对您有所帮助
  • malloc 不是必需的,只要 MAXL 和 MAXC 都是常数。
  • @SergeBallesta.:我已经修改了它..现在检查一下。只是给出了大致的想法。
【解决方案2】:

指针数组与二维数组不是同一个字符串,即使它们用于获取值也是如此。为了能够交换字符串,您必须有一个指针数组。这是您的代码的注释快速修复,因为您离它不远:

void Q5(){
  char str[MAXL][MAXC]; // Ok a 2D array is nice to read the string
  int i;
  char* p[MAXL]; // the array of MAXL pointers
  printf("Please enter %d Strings max length = %d\n",MAXL,MAXC);
  for (i = 0; i < MAXL; i++){
    scanf("%s", str[i]);       // the string is read
    p[i] = str[i];             // and the array of pointers is initialized
  }
  sort_String(p,MAXL);
  printf("\n");
  for (i = 0; i < MAXL; i++){
    printf("%s", p[i]);       // only p has been sorted, str is unchanged
    printf("\n");
  }
}

void sort_String(char** str,int size){ // takes an array of pointers as first parameter
char* tempP;
int i, j;
for (i=0; i < size;i++){   // ok for a bubble sort
    for (j=i+1; j < size; j++){
        if (strlen(str[i]) > strlen(str[j])){  // compare length of pointed strings
            tempP = str[j];             // swap
            str[j] = str[i];
            str[i] = tempP;
        }
    }
}

我假设 MAXLMAXC 都是常量值 (#define)

【讨论】:

  • @coderredoc:同意:即使 MAXL 和 MAXC 是变量,你的也可以工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-08-05
  • 2021-06-28
  • 2010-09-12
  • 2021-12-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多