0

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]);
    }
}
2
  • arr is nothing but &arr[0]. you can either do test(&arr[0], n); or test(arr, n);. As well as void test(int *d, int n) or void test(int d[], int n) Commented Mar 2, 2022 at 8:47
  • An array, when passed to a function, becomes implicitly a pointer. This is known as the array decaying to a pointer, this is a term you'll often come across. This implies two things: an array function parameter is mere syntactic sugar for a pointer argument, and you do not need to take a pointer to the array to pass it to the function. As TruthSeeker said, void test(int *d, int n) is fine, call it with test(arr, n) simply. Commented Mar 2, 2022 at 8:48

2 Answers 2

2

How do you pass an array to a function

Just by using a pointer of the array element type:

void test(int *a, int b);

If you then pass an array to the function:

test(arr);

... the C compiler will pass a pointer to the first element of the array (&(arr[0])) to the function.

Note that you don't use the & in this case.

Inside the function you can use array operations:

void test(int * arr)
{
    arr[3] = arr[2];
}

(In your case: printf("%d\n", arr[n]);)

(This is true for any kind of pointer data type with exception of void *. The C compiler assumes that the pointer points to the first element of an array if you use array operations with pointer data types.)

"passing argument 1 of 'test' from incompatible pointer type"

As far as I know, [] in a function argument is not interpreted as array, but as pointer. For this reason, ...

void test(int *a[], int b);

... is interpreted as:

void test(int **a, int b);

... which means that the C compiler expects a pointer to an array of pointers (int *), not to an array of integers (int).

Sign up to request clarification or add additional context in comments.

Comments

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]);
    }
}

1 Comment

@rnicolas No. Study "array decay" in your favorite C beginner learning material.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.