Pass pointer of array to function that sorts numbers

Taji

I'm new to C and cannot figure out how to pass a pointer of an array to a function. The function should sort user input numbers in ascending order. I think I've missed something crucial in the function.

I'm able to input the user values but that's as far as I've been able to get without errors.

#include <stdio.h>
int sort(int *p, int i); //function declaration
int main()
{
    int numbers[10]; // ten element array
    int i;

    printf("Please enter ten integer values:\n");
    for(i=0; i<10; i++)
        scanf("%d", (&numbers[i]));

    int *p= &numbers; //a pointer that points to the first element of number
    sort(int *p, int i); //function

}

//function sorts in ascending order
int sort (int *p, int i) //function definition
{
    for (i=0; i<10; i++) //loop through entire array
    {
        printf("%d\n", *p);
    }
    return 0;
}
Vlad from Moscow

You should write

int *p= numbers;//a pointer that points to the first element of number
    sort(p, i); //function

An array passed to a function is converted implicitly to pointer to its first element.

Also the function should look like

//function sorts in ascending order
int sort (int *p, int n) //function definition
{
    for ( int i = 0; i < n; i++) //loop through entire array
    {
        printf("%d\n", *p++);
        // or
        //printf("%d\n", p[i]);
    }

    return 0;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Pass char pointer array to function

From Dev

Pass char pointer/array to a function

From Dev

how do i pass an array with a pointer into a function?

From Dev

how do i pass an array with a pointer into a function?

From Dev

Pass a pointer of an array to a function to use in printf` in C

From Dev

How to pass pointer to array of byte to function?

From Dev

pass "pointer to pointer" to a template function

From Dev

When to call function that sorts Array in Swift 4?

From Dev

Why can't I pass the address of a pointer to a fixed size array into a function expecting a pointer to a pointer in C?

From Dev

How to pass a float array to the function that has unsigned char pointer as an argument?

From Dev

Pass pointer to data as argument to function expecting two-dimensional array

From Dev

C - Pass a pointer on bi-dimensional array to a function

From Dev

C - Pass a pointer on bi-dimensional array to a function

From Dev

How to pass a pointer to a function argument of type fixed size array?

From Dev

Pass pointer by reference to function

From Dev

Pass on function arguments by pointer

From Dev

Pass pointer to function

From Dev

Pass typedefed function pointer

From Dev

Pass 2d array in function: 2d array received is pointer array

From Dev

Finding the primary numbers in an array of pointer

From Dev

C pass pointer as argument in function

From Dev

Pass a function pointer to a class object

From Dev

Pass a pointer to a function that receives a reference?

From Dev

when to pass a pointer argument to a function

From Dev

Pass char pointer to function in C

From Dev

How to pass a character pointer to a function

From Dev

Passing a pointer to an array to a function

From Dev

Function returning a Pointer to an Array

From Dev

Pointer to array of struct in function

Related Related

  1. 1

    Pass char pointer array to function

  2. 2

    Pass char pointer/array to a function

  3. 3

    how do i pass an array with a pointer into a function?

  4. 4

    how do i pass an array with a pointer into a function?

  5. 5

    Pass a pointer of an array to a function to use in printf` in C

  6. 6

    How to pass pointer to array of byte to function?

  7. 7

    pass "pointer to pointer" to a template function

  8. 8

    When to call function that sorts Array in Swift 4?

  9. 9

    Why can't I pass the address of a pointer to a fixed size array into a function expecting a pointer to a pointer in C?

  10. 10

    How to pass a float array to the function that has unsigned char pointer as an argument?

  11. 11

    Pass pointer to data as argument to function expecting two-dimensional array

  12. 12

    C - Pass a pointer on bi-dimensional array to a function

  13. 13

    C - Pass a pointer on bi-dimensional array to a function

  14. 14

    How to pass a pointer to a function argument of type fixed size array?

  15. 15

    Pass pointer by reference to function

  16. 16

    Pass on function arguments by pointer

  17. 17

    Pass pointer to function

  18. 18

    Pass typedefed function pointer

  19. 19

    Pass 2d array in function: 2d array received is pointer array

  20. 20

    Finding the primary numbers in an array of pointer

  21. 21

    C pass pointer as argument in function

  22. 22

    Pass a function pointer to a class object

  23. 23

    Pass a pointer to a function that receives a reference?

  24. 24

    when to pass a pointer argument to a function

  25. 25

    Pass char pointer to function in C

  26. 26

    How to pass a character pointer to a function

  27. 27

    Passing a pointer to an array to a function

  28. 28

    Function returning a Pointer to an Array

  29. 29

    Pointer to array of struct in function

HotTag

Archive