#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/