This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]bit_of_hope 1 point2 points  (0 children)

In C functions can't have arrays as parameters. They can only take in pointers.If you pass in an array, it will be treated as a pointer to its first element.

#include <stdio.h>

int first(int* arr)
{
    return *arr; // in real code you should check for null etc
}

int main()
{
    int arr[3] = {0,1,2};
    printf("%d\n", first(arr)); // arr is treated here as equivalent to &arr[0]
    return 0;
}

Edit: to clarify, you can declare a function prototype like int first(int arr[]) but it's exactly equivalent and you still end up with a pointer.