0

New learner here; I am performing a traverse on any given array but I find I get this error:

exe_3.c:18:27: warning: incompatible pointer types passing 'int *' to parameter of type 'int **' [-Wincompatible-pointer-types]
    int result = traverse(&arr[6], &n);
                          ^~~~~~~
exe_3.c:4:25: note: passing argument to parameter 'A' here
const int traverse(int *A[], int *N){

What I have tried:

#include <stdio.h>
#include <stdlib.h>

const int traverse(int *A[], int *N){
    int i = 0;
    int arr[*N];
    while(i < *N){
        arr[i] = *A[i];
        i += 1;
    }
    return *arr;
}

int main(){
    int arr[6] = {1, 2, 3, 4, 5, 6};
    int n = sizeof(arr)/sizeof(arr[0]);

    int result = traverse(&arr, &n);
    printf("%i\n", result);
    return EXIT_SUCCESS;
}
Stackcans
  • 351
  • 1
  • 9
  • 2
    An array is passed to a function as a pointer to the first element, so there is no reason to add `&`. Also `int * arr[]` would be an array of pointer, not an array of ints. – stark Nov 30 '22 at 19:20
  • 1
    @Stackcans It is unclear what you are trying to achieve with this incorrect program.:) What is the purpose of the function? – Vlad from Moscow Nov 30 '22 at 19:42
  • @habrewning, a pointer to an array of `int` is passed as a pointer to an array of `int` -- that is, `int (*)[some_dimension]`. An `int **` is something else entirely. – John Bollinger Nov 30 '22 at 19:51
  • Since you only return one `int` from the function (the one in `main::arr[0]`), why do you loop over all the others and put them in a temporary array inside the function? – Ted Lyngmo Nov 30 '22 at 19:52
  • 2
    You don't need to pass a pointer to the array length. The length itself is good, because the function does not modify it. – Weather Vane Nov 30 '22 at 19:53

1 Answers1

1

Your call traverse(&arr, &n); passes a pointer to the array arr to the function traverse.

You get the error message, because the correct type definition for a pointer to an array of integers is int(A*)[]. You have that type in the definition of the traverse function incorrect (your line 4).

You will see that this is not enough to compile your code. When accessing the elements of such an array via that pointer you need the expression (*A)[i]. You have that access in the implementation of the traverse function incorrect (your line 8).

See also here for more details: C pointer to array/array of pointers disambiguation

What I find also strange with your traverse function is that the array arr is not used completely. Only the first value is returned. I suppose your code is just not complete.

habrewning
  • 735
  • 3
  • 12
  • That was definitely helpful and using parentheses made the programme work! although, your point on `i`, I find that when I put this above the array line and run the console, it produces a new 8 digit number each time. What is happening? – Stackcans Nov 30 '22 at 22:08
  • Sorry, my statement about `i` was incorrect. I am removing it. – habrewning Dec 01 '22 at 06:13