如何将排序结构与字符串合并?

How to merge sort struct with strings?

我正在尝试修改一个通用的合并排序,用于对字符串结构进行排序。但我不知道出了什么问题。带有 strcpy() 和 strcmp() 的每一行都会出现一条警告,告诉您:

warning: passing argument 1 of 'strcpy' makes pointer from integer without a cast [-Wint-conversion]|
C:\Program Files (x86)\CodeBlocks\MinGW\include\string.h|45|note: expected 'char *' but argument is of type 'char'|

它建议我把 * 放在 char L[n1] 和 R[n2]

But even if i declare 
char *L[n1] 
char *R[n2]

它总是在 strcpy() 或什至在 strcmp() 处停止。 . .我不明白为什么。有人知道如何使用 strcpy 和 strcmp 这两个吗?

struct kegiatan{

     char nama[50];
};

void merge(struct kegiatan *keg, int low, int mid, int high){

    int i, j, k;

    int n1 = mid - low + 1;
    int n2 = high - mid;
    char L[n1];
    char R[n2];

    for(i = 0; i < n1; i++){
        strcpy(L[i], keg[low + i].nama);        // the program always stops here.
    }
    printf("L = %s", L[i]);
    printf("keg = %s", keg[low + i].nama);
    system("pause");
    for(j = 0; j < n2; j++){
        strcpy(R[j], keg[mid + 1 + j].nama);
    }

    i = 0;
    j = 0;
    k = low;

    while(i < n1 && j < n2){
        if(strcmp(L[i], R[j]) < 0){
            strcpy(keg[k].nama, L[i]);
            i++;
        } else{
            strcpy(keg[k].nama, R[j]);
            j++;
        }
        k++;
    }
     while (i < n1) { 
        strcpy(keg[k].nama, L[I]); 
        i++; 
        k++; 
     }

    while(j < n2) {
        strcpy(keg[k].nama, R[j]);
        j++;
        k++;
    }

}

void mergesort(struct kegiatan *keg, int low, int high){

    if(low < high){
        int mid = (low + high) / 2;

        mergesort(keg, low, mid);
        mergesort(keg, mid + 1, high);
        merge(keg, low, mid, high);
    }

}

您将字符串误认为是 char。这里 strcpy(L[i], keg[low + i].nama); 您不是将一个字符串复制到另一个字符串。您是将一个字符串复制到数组 L 的一个元素。

还请注意,在 printf 中,您不应将 %s 用于 L[i],并且不要在 strcpy 中使用 R[j](它是一个字符) .

注意你在这里比较两个字符 if(strcmp(L[i], R[j]) ,所以不要使用 strcmp.

简而言之,当你声明char string[num]时,它是一个字符数组,但是这个string[i]不是一个数组,它只是array.so的一个元素,不要把它当作一个数组。

请注意,在您的所有 strcpy 中,您都发送了一个字符串和一个字符串元素作为参数。

考虑使用 <stdlib.h> 中的内置 qsort 函数。

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

struct sample {
    char *word;
};

int comparator(const struct sample *a, const struct sample *b) {
    return strcmp(a->word, b->word);
}

int main() {
    struct sample s[] = {{"n"},
                         {"b"},
                         {"a"}};
    int elements = sizeof(s) / sizeof(struct sample);
    qsort(s, elements, sizeof(struct sample), (__compar_fn_t) comparator);
    for (int i = 0; i < elements; ++i) {
        printf("%s\n", s[i].word);
    }
    return 0;
}

首先我在你的代码中看到:

printf("L = %s", L[i]);
printf("keg = %s", keg[low + i].nama);

L[i] 是字符类型,不是字符串作为@Hanie 的答案。这里的另一个错误是您打印了数组容量之外的 L[i],因为在 for 循环之后,i 等于 n1。这就是为什么您的程序有时会在这里停止的原因。如果要一步步看字符串'L[i]',就放在for循环中。

其次,在您的合并代码中,您忘记将 L[] 的剩余元素复制为以下代码。

 while (i < n1) 
 { 
     strcpy(keg[k].nama, L[i]); 
     i++; 
     k++; 
 }

如果使用声明

char *L[n1] 
char *R[n2]

您必须为 strcpy 之前的字符串分配 (malloc)。或者您可以声明为 char L[n1][50], L2[n2][50]

完整代码:

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
struct kegiatan{

     char nama[50];
};

void merge(struct kegiatan *keg, int low, int mid, int high){

    int i, j, k;

    int n1 = mid - low + 1;
    int n2 = high - mid;
    char L[n1][50];
    char R[n2][50];

    for(i = 0; i < n1; i++){
        strcpy(L[i], keg[low + i].nama); 
        //printf("L[%d] = %s\n", i, L[i]); 
    }
    for(j = 0; j < n2; j++){
        strcpy(R[j], keg[mid + 1 + j].nama);
    }

    i = 0;
    j = 0;
    k = low;

    while(i < n1 && j < n2){
        if(strcmp(L[i], R[j]) <= 0){
            strcpy(keg[k].nama, L[i]);
            i++;
        } else{
            strcpy(keg[k].nama, R[j]);
            j++;
        }
        k++;
    }

     // Copy the remaining elements of L[]. Here you have to add
     while (i < n1) 
    { 
        strcpy(keg[k].nama, L[i]); 
        i++; 
        k++; 
    } 

    while(j < n2){
        strcpy(keg[k].nama, R[j]);
        j++;
        k++;
    }

}

void mergesort(struct kegiatan *keg, int low, int high){

    if(low < high){
        int mid = (low + high) / 2;

        mergesort(keg, low, mid);
        mergesort(keg, mid + 1, high);
        merge(keg, low, mid, high);
    }

}

int main () {
    struct kegiatan st[3];
    strcpy(st[0].nama,"something2");
    strcpy(st[1].nama,"something1");
    strcpy(st[2].nama,"something3");
    mergesort(st, 0, 2);

    for (int i = 0; i < 3; i++) {
        printf("%s\n", st[i].nama);
    }
}

结果:

something1
something2
something3