2
void test(char **arr)
{
    // do something
}

int main()
{
    char arr[10][25];
    test(arr);
}

When I try to compile this using gcc v8.2.1, it gives me the following errors:

prog.c:8:8: warning: passing argument 1 of ‘test’ from
incompatible pointer type [-Wincompatible-pointer-types]
  test(arr);
       ^~~ 
prog.c:1:19: note: expected ‘char **’ but argument is of type ‘char (*)[25]’
  void test(char **arr)
            ~~~~~~~^~~

This is for homework, so I am not allowed to change the test function to take an array (rather than a pointer to a pointer) and I need to ensure the array declared in main have those dimensions (no more and no less).

How should I go about doing this?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
JolonB
  • 415
  • 5
  • 25
  • Just to add, it sounds like your school is teaching you to make jagged arrays of dynamically allocated sub-arrays... not good. I'm sorry. – Lightness Races in Orbit Mar 12 '19 at 23:30
  • What should I do instead? I am being forced to use the pointers (we aren't allowed to change that), but we can make the array however we want. We haven't learnt about pointers yet, and the array needs to have a fixed size. – JolonB Mar 12 '19 at 23:48
  • Why are you being forced to use pointers if you haven't learnt about pointers yet? Sounds to me like you need to ask your teacher for clarification on their expectations. – Lightness Races in Orbit Mar 12 '19 at 23:59
  • I was thinking that too. In the assignment, there is also a bit where we pass a 1D array (declared: `char arr[25]`) into a method and the method accepts `char *arr`. This works perfectly find. I thought it was weird that one was working but the other wasn't, while at the same time the assignment makes no mention of them being any different. – JolonB Mar 13 '19 at 00:02
  • This is quite a subtle thing of C and C++ but there are good reasons for it. It's strange to me that you haven't been taught it yet if you are expected to write a project on it. – Lightness Races in Orbit Mar 13 '19 at 00:06
  • I guess I might email the lecturer in that case. I thought it was something I should've understood, but it seems to be a lack of teaching on his behalf. Thanks – JolonB Mar 13 '19 at 05:25

0 Answers0