Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Passing arrays to functions in C?

i read that you use a pointer to do that job, but honestly, i had trouble understanding what i read. i would be grateful if someone could explain how you pass an array to a function in C (if it makes any difference i'm working with ANSI C99)

so for the sake of example:

int function(i don't know what goes here, if anything)

{

some code to process array

}

function(pointer to array. not sure how this goes either);

4 Answers

Relevance
  • ?
    Lv 7
    1 decade ago
    Favorite Answer

    A lot of people will tell you "array is a pointer", but that's not true at all. Array is a type.When you write

    int a[3] = {1,2,3};

    you now have an object called a which has the type array-of-3-ints. This is NOT a pointer.

    However, in C, such objects are not allowed as function arguments. There are two different ways to pass an array to a function:

    1) as a pointer to array:

    #include <stdio.h>

    void function(int (*a)[3])

    {

         for(int i=0; i<3; ++i)

             printf("%d ", (*a)[i]);

         putchar('\n');

    }

    int main()

    {

         int a[3] = {1,2,3};

         function(&a);

    }

    In the first example I'm simply converting the object a to a pointer by the address-of operator, and pass that pointer-to-array-of-3-ints to the function.

    2) as a pointer to the first element of the array:

    #include <stdio.h>

    void function(int* a, int N)

    {

         for(int i=0; i<N; ++i)

             printf("%d ", a[i]);

         putchar('\n');

    }

    int main()

    {

         int a[3] = {1,2,3};

         function(&a[0], 3);

    }

    C programming language offers a way to abbreviate the second approach. It can be written as:

    int main()

    {

         int a[3] = {1,2,3};

         function(a, 3);

    }

    In this code, it seems as if the array name is used directly. However C compiler cannot accept an array as a function argument, so it tries possible implicit conversions. One of the implicit conversions that C offers for arrays is known as "decay to pointer": function(a, 3); is reinterpreted by the compiler as "function(&a[0], 3);" and if such call is possible (function accepts a pointer-to-int as its first argument), it is made.

  • 1 decade ago

    Okay, in general if you declare int array[30]; then array is the same as &array[0] or it is a pointer to the address of array[0], the first element. As another website puts it:

    The famous "decay convention": an array is

    treated as a pointer that points to the

    first element of the array.

    The decay convention shouldn't be applied

    more than once to the same object.

    Taken from here:

    http://www.ibiblio.org/pub/languages/fortran/appen...

    Generally in the main body of a program if you want to send a pointer to a function you can often just use the address of operator (&) on a variable in main() or whatever the calling function is. In the case of arrays, just declare them, usually and they will be passed as pointers, not as copies the way other variables would be because that would take up too much memory, especially for the older compilers.

  • 1 decade ago

    When you declare an array of whatever datatype, what actually gets stored in the array variable is the address of the first element of the array. In other words, an array is actually a pointer.

    For example, let's say in the main function, you declare an array of ints:

    int array[] = {5, 2, 6, 9, 4, 2, 3, 1, 5, 7};

    This is equivalent to saying:

    int *array = {5, 2, 6, 9, ... etc

    Now, you want to pass this into a function called sort. Since array is already a pointer, you just need to pass array as the argument of the function.

    void sort(int list[])

    {

    . . . // sorting algorithm here

    }

    In main:

    sort(array);

    ------

    On another note, you probably will need to pass the length of the array as another argument for whatever you need to do in the function.

    So, your function might look like:

    int function(int array[], int length);

Still have questions? Get your answers by asking now.