c - 尝试按地址将数组传递给函数来对数组进行排序时出现段错误 11

标签 c pointers

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

void arrayCopy(int* fromArray[], int* toArray[], int size);  
void sort (int* arr[], int size);

int main(void){
    int initialArray[] = {540, 2100, 5150, 3};
    int size = sizeof(initialArray) / sizeof(initialArray[0]); //get length of array
    int copiedArray[size]; //make new array at size of initial array

    arrayCopy(&initialArray, &copiedArray, size); //copy 

    for(int i = 0; i < size; i++){
        printf("initialArray, %d: %d -> copiedArray, %d: %d\n", i, initialArray[i], i,  copiedArray[i]);
    }

    printf("\n");

    sort(&copiedArray, size);

    printf("Sorted array: {");
    for(int i = 0; i < size; i++){
        printf("%d,", copiedArray[i]);
    }

    printf("}\n");

    return 0;
}

/*

copies all the elements from one array to another

*/

void arrayCopy(int* fromArray[], int* toArray[], int size) {
    for(int i = 0; i < size; i++){
        *toArray[i] = *fromArray[i];
    }
}

/*

sorts a given array in ascending order

*/

void sort(int* arr[], int size) {
    for(int i = 0; i < size - 1; i++){
        if(*arr[i] < *arr[i + 1]){
            int temp = *arr[i + 1];
            *arr[i + 1] = *arr[i];
            *arr[i] = temp;
        }
    }
}

我正在尝试制作一个简单的应用程序来学习 C 中的地址/引用传递。我创建了一个函数来将 1 个数组的内容复制到另一个数组。第二个功能是对新创建的数组进行排序。当我尝试运行它时,它会在编译后给出段错误错误和以下警告:

proj1.c:12:12: warning: incompatible pointer types passing 'int (*)[4]' to parameter of type 'int **' [-Wincompatible-pointer-types]
    arrayCopy(&initialArray, &copiedArray, size); //copy 
              ^~~~~~~~~~~~~
proj1.c:4:21: note: passing argument to parameter 'fromArray' here
void arrayCopy(int* fromArray[], int* toArray[], int size);
                ^
proj1.c:12:27: warning: incompatible pointer types passing 'int (*)[size]' to parameter of type 'int **' [-Wincompatible-pointer-types]
    arrayCopy(&initialArray, &copiedArray, size); //copy 
                             ^~~~~~~~~~~~
proj1.c:4:39: note: passing argument to parameter 'toArray' here
void arrayCopy(int* fromArray[], int* toArray[], int size);
                                  ^
proj1.c:20:7: warning: incompatible pointer types passing 'int (*)[size]' to parameter of type 'int **' [-Wincompatible-pointer-types]
    sort(&copiedArray, size);
         ^~~~~~~~~~~~
proj1.c:5:17: note: passing argument to parameter 'arr' here
void sort (int* arr[], int size);
            ^
3 warnings generated.

问题是什么?

最佳答案

根据您的arrayCopy()声明编译器会将第一个参数视为双指针 *fromArray地址为initialArray**fromArray将包含540在函数定义中,您试图遵循 540地址无效,因此更改 void arrayCopy(int* fromArray[], int* toArray[], int size); -> void arrayCopy(int fromArray[], int toArray[], int size);并检查以下修改后的程序。

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

void arrayCopy(int fromArray[], int toArray[], int size);  
void sort (int arr[], int size);

int main(void){
    int initialArray[] = {540, 2100, 5150, 3};
    int size = sizeof(initialArray) / sizeof(initialArray[0]); //get length of array
    int copiedArray[size]; //make new array at size of initial array
    int i;

    arrayCopy(initialArray, copiedArray, size); //copy 

    for(i = 0; i < size; i++){
        printf("initialArray, %d: %d -> copiedArray, %d: %d\n", i, initialArray[i], i,  copiedArray[i]);
    }

    printf("\n");

    sort(copiedArray, size);

    printf("Sorted array: {");
    for(i = 0; i < size; i++){
        printf("%d,", copiedArray[i]);
    }

    printf("}\n");

    return 0;
}

/*

copies all the elements from one array to another

*/

void arrayCopy(int fromArray[], int toArray[], int size) {
    int i;
    for(i = 0; i < size; i++){
        toArray[i] = fromArray[i];
    }
}

/*

sorts a given array in ascending order

*/

void sort(int arr[], int size) {
    int i;
    for(i = 0; i < size - 1; i++){
        if(arr[i] < arr[i + 1]){
            int temp = arr[i + 1];
            arr[i + 1] = arr[i];
            arr[i] = temp;
        }
    }
}

关于c - 尝试按地址将数组传递给函数来对数组进行排序时出现段错误 11,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32451128/

相关文章:

c - 在 C 中确定性地生成强力排列

c - 我无法使用 scanf 将值放入动态分配的数组的元素中?

c - 为什么我不能编辑 char* 中的 char?

c++ - 可以指向多个类的指针

c - 从数组中删除前 X 个元素

c++使用字符串的动态数组不接受字符串

c - MPI Gather 仅从根进程收集

c - 从/dev/input/event读取

C:从 void 指针推断类型

c - 返回指向数组的指针