Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pointers to arrays problem in C

Tags:

arrays

c

pointers

I have this function that takes a pointer of an array (in order to modify it from within the function)

int func_test(char *arr[]){
    return 0;
}

int main(){
  char var[3];
  func_test(&var);

  return 0;
}

When I try to compile this I get :

passing argument 1 of ‘func_test’ from incompatible pointer type

Why is this problem, and how I pass a pointer to that array in this case?

like image 597
user824906 Avatar asked Aug 28 '26 05:08

user824906


1 Answers

char * arr[] is not a pointer to an array; it is an array of pointers. Declarations in C are read first from the identifier towards the right, then from the identifier towards the left. So:

    char * arr[];
//         ^ arr is...
//            ^ an array of...
//       ^ pointers to...
//  ^ char
like image 85
Aasmund Eldhuset Avatar answered Sep 06 '26 20:09

Aasmund Eldhuset



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!