我正在try 合并 struct 数组的排序,我有许多不同的seg错误,但目前它发生在arrayCopy函数中:

void arrayCopy(item10array dest[], item10array src[], const int lenght, const int leftShift) {
    for (int i = 0; i < lenght; i++) {
        structCopy(dest[i], src[leftShift + i]);
    }
}

我已经改变了一些小事情,直到我把seg错误推入函数arrayCopy,我不明白是什么导致了这个错误.

typedef struct 
{
    char Nome[50]; //vem do ficheiro ruas.txt
    unsigned int int_arr[6];
    char char_arr[6];
} Arteria;

typedef struct
{
    Arteria arteria;
    int cont;
} item10array;

void structCopy(item10array dest, item10array src) {

    strcpy(dest.arteria.Nome, src.arteria.Nome);

    for (int j = 0; j < 6; j++) {
        dest.arteria.int_arr[j] = src.arteria.int_arr[j];
        dest.arteria.char_arr[j] = src.arteria.char_arr[j];
    }

    dest.cont = src.cont;
}

void arrayCopy(item10array dest[], item10array src[], const int lenght, const int leftShift) {
    for (int i = 0; i < lenght; i++) {
        structCopy(dest[i], src[leftShift + i]);
    }
}

void mergeSorted(item10array *array, const int l, const int m, const int r) {
    int left_lenght = m - l + 1;
    int right_lenght = r - m;

    item10array temp_left[left_lenght];
    item10array temp_right[right_lenght];

    int i, j, k;

    arrayCopy(temp_left, array, left_lenght, l);
    arrayCopy(temp_right, array, right_lenght, m + 1);

    for (i = 0, j = 0, k = l; k <= r; k++) {
        if ((i < left_lenght) && (j >= right_lenght || temp_left[i].cont <= temp_right[j].cont)) {
            structCopy(array[k], temp_left[i]);
            i++;
        } else {
            structCopy(array[k], temp_right[j]);
            j++;
        }
    }  
}

void sortRecursion(item10array *arrray, int l, int r) {
    //isto é desnecessario mas dá me paz de alma
    if (r <= 1)
        return;
    if (l < r) {
        //encontrar o meio do array
        int m = l + (r - l) / 2;

        sortRecursion(arrray, l, m);
        sortRecursion(arrray, m + 1, r);

        mergeSorted(arrray, l, m, r);
    }
}

void sortItem10(item10array *rua, int lenght) {
    sortRecursion(rua, 0, lenght - 1);
}

void item10letra(Arteria *origin_ptr) {
    item10array rua[345] = {{"0", 0, 0, 0}};
    for (int i = 0; i < 345; i++) {
        rua[i].arteria = origin_ptr[i];
    }
    char letra;
    int cont = 0;
    int lenght = sizeof(item10array) * 345;

    for (int i = 0; i < 345; i++) {
        rua[i].arteria = origin_ptr[i];
    }

    for (int i = 0; i < 345; i++) {
        for (int j = 0; j < 6 && rua[i].arteria.char_arr[j] != 0; j++) {
            if (rua[i].arteria.char_arr[j] == letra) {
                cont++;
            }
        }
        rua[i].cont = cont;
    }

    sortItem10(rua, lenght);
}

有些东西不是用英文命名的,因为我也不是用英文命名的.

谢谢.

推荐答案

如前所述,structCopy不会做任何有用的事情,因为srcdest struct 是通过值传递的,所以将src的成员赋给dest的成员只会修改函数参数,而不会影响传递给arrayCopy的 struct array.

只需通过赋值即可复制 struct .以下是arrayCopy的修改版本:

void arrayCopy(item10array dest[], const item10array src[],
               int length, int leftShift) {
    for (int i = 0; i < length; i++) {
        dest[i] = src[leftShift + i];
    }
}

您的排序程序中的问题出在其他地方:

  • sortRecursion中计算的length是不正确的:int length = sizeof(item10array) * 345;是假的,因为您将以字节为单位的数组大小乘以其硬编码的元素数,而不是除以数组元素的大小:

    int length = sizeof(item10array) / sizeof(item10array[0]);
    
  • 另外,char letra;未被初始化,因此计算该字母在动脉名称中的出现是没有意义的.

  • 还注意,使用r作为切片中最后一个元素的索引也会引起混淆.在C语言中,更习惯的做法是使用超过切片末尾的元素的索引,以避免容易出错的+1 / -1调整.

以下是代码的修改版本:

typedef struct {
    char Nome[50]; //vem do ficheiro ruas.txt
    unsigned int int_arr[6];
    char char_arr[6];
} Arteria;

typedef struct {
    Arteria arteria;
    int cont;
} item10array;

void arrayCopy(item10array dest[], const item10array src[],
               int length, int leftShift) {
    for (int i = 0; i < length; i++) {
        dest[i] = src[leftShift + i];
    }
}

void mergeSorted(item10array *array, int l, int m, int r) {
    int left_length = m - l;
    int i, j, k;
    item10array temp_left[left_length];

    // only save the left part, elements in the right part are
    // never overwritten before they are copied.
    arrayCopy(temp_left, array, left_length, l);

    for (i = 0, j = m, k = l; k < r; k++) {
        if (i < left_length
        &&  (j >= r || temp_left[i].cont <= array[j].cont)) {
            array[k] = temp_left[i++];
        } else {
            array[k] = array[j++]);
        }
    }  
}

void sortRecursion(item10array *array, int l, int r) {
    if (r - l > 1) {
        // encontrar o meio do array
        int m = l + (r - l) / 2;

        sortRecursion(array, l, m);
        sortRecursion(array, m, r);
        mergeSorted(array, l, m, r);
    }
}

void sortItem10(item10array *rua, int length) {
    sortRecursion(rua, 0, length);
}

void item10letra(Arteria *origin_ptr) {
    item10array rua[345] = {{"0", 0, 0, 0}};
    int length = sizeof(item10array) / sizeof(item10array[0]);
    char letra = 'a';

    for (int i = 0; i < length; i++) {
        int cont = 0;
        rua[i].arteria = origin_ptr[i];
        for (int j = 0; j < 6 && rua[i].arteria.char_arr[j] != 0; j++) {
            if (rua[i].arteria.char_arr[j] == letra) {
                cont++;
            }
        }
        rua[i].cont = cont;
    }
    sortItem10(rua, length);
}

C++相关问答推荐

理解C中的指针定义

GCC不警告隐式指针到整数转换'

编译SDL 2时缺少SDL_ttf

exit(EXIT_FAILURE):Tcl C API类似功能

C中是否有语法可以直接初始化一个常量文本常量数组的 struct 成员?

#If指令中未定义宏?

Rust FFI--如何用给出返回引用的迭代器包装C风格的迭代器?

GDB输出ARM助记符

有什么方法可以将字符串与我们 Select 的子字符串分开吗?喜欢:SIN(LOG(10))

为什么用非常数指针变量改变常量静态变量时会出现分段错误?

类型定义 struct 与简单的类型定义 struct

Go和C中的数据 struct 对齐差异

为什么Linux无法映射这个PT_LOAD ELF段?

使用正则表达式获取字符串中标记的开始和结束

如何修复我的qsort()算法?它每次都给出不同的结果

用于计算位数和的递归C函数

传递给函数的 struct 中的数组

在分配内存后使用指针是未定义的行为吗?

子进程不会修改父进程中的统计信息

为什么实现文件中的自由函数默认没有内部链接?