'How do you pass an array to a function using pointers

I am trying to print an array through a function by using call by reference but keep getting a warning:

passing argument 1 of 'test' from incompatible pointer type [-Wincompatible-pointer-types]

I tried replacing test(&arr, n); with test(arr, n);, test(*arr, n);, test(&arr[], n);, test(*arr[], n);, test(&arr[], n);

but nothing worked, what am I doing wrong?

#include<stdio.h>
void test(int *a[], int b);
void main()
{
    int arr[]={1, 2, 3, 4, 5}, i, n=5;
    test(&arr, n);
}
void test(int *d[], int n)
{
    int i;
    for(i=0; i<n; i++)
    {
        printf("%d", *d[i]);
    }
}


Solution 1:[1]

It's much simpler than that. In the example below I'm using size_t to express array size, it's an unsigned integer type specifically meant to be used for that purpose.

#include <stdio.h>

// you can use the size parameter as array parameter size
void test (size_t size, int arr[size]); 

int main (void) // correct form of main()
{
    int arr[]={1, 2, 3, 4, 5};
    test(5, arr);
}

void test (size_t size, int arr[size])
{
    for(size_t i=0; i<size; i++) // declare the loop iterator inside the loop
    {
        printf("%d ", arr[i]);
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Lundin