Function and arrays

How do you make a function accept an array and the size of the array as parameters?
I think you can do something like this but haven't tested it.
1
2
3
4
5
6
7
int x=0;
int array_p[5]={0};
x=sizeof(array_p)/sizeof(int);
void print_array_p(int array_p[], int x)
     {
         //code goes here
     } 
@buffbill: Umm.... I'm not sure what you were trying to do there?

An array is simply a pointer to a segment of memory with several variables stored with in it, so to pass an array, the function needs to take a pointer as the first parameter:

1
2
3
4
5
6
7
void Func(int* array, int size)
{
    for (int i = 0; i<size; i++)
    {
        array[i] = i; //array[i] is the same as *(array+i)
    }
}
Topic archived. No new replies allowed.