简体   繁体   English

使用带数组的指针时遇到麻烦

[英]Trouble using pointers with array

I'm new to C and having a problem with pointers. 我是C的新手并且指针有问题。 I've been unable to find the answer online or through my peers, so here I am. 我无法在线或通过我的同行找到答案,所以我在这里。

I've been given an assignment to: 我被赋予了一个任务:

  • Create an array of 20 random integers 创建一个包含20个随机整数的数组
  • print out the integers 打印出整数
  • sort the array in ascending order 按升序对数组进行排序
  • print it out again 再次打印出来

When I compile the program with GCC and run, I get a segmentation fault. 当我用GCC编译程序并运行时,我得到了一个分段错误。 I've narrowed it down to happening when I try and set the value of number[i] or number[k] in the sort function. 当我尝试在sort函数中设置number[i]number[k]的值时,我已经缩小了它的范围。 Any help would be appreciated. 任何帮助,将不胜感激。

#include <stdio.h>

void sort(int* number, int n){
     /*Sort the given array number , of length n*/
    int temp, min;
    int i, k;
    for(i=0; i<n; i++){
        min = i;
        for(k=i+1; k<n; k++){
            if(number[k]<min){
                min = k;
            }
        }
        temp = number[i];
        number[i] = number[k];
        number[k] = temp;
    }   
}

int main(){
    /*Declare an integer n and assign it a value of 20.*/
    int n=20;

    /*Allocate memory for an array of n integers using malloc.*/
    int *array = malloc(n * sizeof(array));

    /*Fill this array with random numbers, using rand().*/
    srand(time(NULL));
    int i;
    for(i=0; i<n; i++){
        array[i] = rand()%1000+1;
    }

    /*Print the contents of the array.*/
    for(i=0; i<n; i++){
        printf("%d\n", array[i]);
    }

    /*Pass this array along with n to the sort() function of part a.*/
    sort(&array, 20);

    /*Print the contents of the array.*/
    printf("\n");
    for(i=0; i<n; i++){
        printf("%d\n", array[i]);
    }

    return 0;
}

Here are the compile errors I get: 以下是我得到的编译错误:

Q3.c: In function âmainâ: Q3.c:在函数中:

Q3.c:31: warning: implicit declaration of function âmallocâ Q3.c:31:警告:隐式声明函数âmallocâ

Q3.c:31: warning: incompatible implicit declaration of built-in function âmallocâ Q3.c:31:警告:内置函数âmalloc的不兼容的隐式声明

Q3.c:34: warning: implicit declaration of function âsrandâ Q3.c:34:警告:隐式声明函数âsrandâ

Q3.c:34: warning: implicit declaration of function âtimeâ Q3.c:34:警告:隐式声明函数âtimeâ

Q3.c:37: warning: implicit declaration of function ârandâ Q3.c:37:警告:隐式声明函数“

Q3.c:46: warning: passing argument 1 of âsortâ from incompatible pointer type Q3.c:46:警告:从不兼容的指针类型传递âsortâ的参数1

Q3.c:9: note: expected âint *â but argument is of type âint **â Q3.c:9:注意:预期âint*â,但参数类型为âint**â

At the point where you swap elements, 在您交换元素的位置,

temp = number[i];
number[i] = number[k];
number[k] = temp;

k == n because it's after the end of k == n因为它是在结束之后

for(k=i+1; k<n; k++){

You meant to use min instead of k in the swap. 你的意思是在交换中使用min而不是k

In main , main

int *array = malloc(n * sizeof(array));

allocates enough space for n pointers to int , not space for 20 int . 分配足够的空间n指针int ,20没有空间int That should be 那应该是

int *array = malloc(n * sizeof *array);

With regard to the compiler warnings/errors, 关于编译器警告/错误,

#include <stdlib.h>

and call 并打电话

sort(array, 20);

instead of passing &array . 而不是传递&array

array是一个int* ,你的sort函数期望一个int* ,你传递&array ,int指针的地址

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM