0

I'm new to C and having a problem with pointers. 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
  • 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. I've narrowed it down to happening when I try and set the value of number[i] or number[k] in the sort function. 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:31: warning: implicit declaration of function âmallocâ

Q3.c:31: warning: incompatible implicit declaration of built-in function âmallocâ

Q3.c:34: warning: implicit declaration of function âsrandâ

Q3.c:34: warning: implicit declaration of function âtimeâ

Q3.c:37: warning: implicit declaration of function ârandâ

Q3.c:46: warning: passing argument 1 of âsortâ from incompatible pointer type

Q3.c:9: note: expected âint *â but argument is of type âint **â

2
  • You don't need dynamic allocation for your problem. An array of 20 integers is int[20]. Commented Apr 15, 2013 at 11:50
  • sizeof(array) is not rite you meant sizeof(*array)?. moreover "<stdlib.h>" is necessary for malloc() Commented Apr 15, 2013 at 11:58

2 Answers 2

2

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

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

You meant to use min instead of k in the swap.

In main,

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

allocates enough space for n pointers to int, not space for 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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, that worked. I also missed that if(number[k]<min) should have been if(number[k]<number[min])
0

array is an int*, your sort function is expecting an int*, yet you pass &array, the address of the int pointer

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.