1

I am trying to create a function in a C program that gets a 2D char array that is dynamically allocated like this:

char **arr;
int N, M;
scanf("%d%d", &N, &M);
arr = malloc(N * sizeof(char *));
if(arr == NULL)
    {
    printf("Not enough memory\n");
    exit(0);
    }
for(i = 0; i < N; i++)
    {
    arr[i] = malloc(M * sizeof(char));
    if(arr[i] == NULL)
        {
        printf("Not enough memory\n");
        exit(0);
        }
    }

And then returns that array. I have declared the function like this(Before typing the code in main).

char check(char **arr);

Then, to edit the function, I have started like this:

check(**arr) {
         /*code here*/
         return **arr;
}

And to call the function, I use this:

check(**arr);

However, when I try to compile it with gcc -ansi -pedantic -Werror, I get these errors/warnings:

file.c: In function ‘main’:
file.c:42:2: error: passing argument 1 of ‘check’ makes pointer from integer without a cast [-Werror]
  check(**arr);
  ^
file.c:6:6: note: expected ‘char **’ but argument is of type ‘char’
 char check(char **arr);
      ^
file.c: At top level:
file.c:57:7: error: expected declaration specifiers or ‘...’ before ‘*’ token
 check(**arr) {
       ^
cc1: all warnings being treated as errors

Line 42 is when I call the check function in main like I said above. Line 6 is when I declare the check function. Line 57 is when I start editing the function like I said above.

I know I should have probably posted the all of the code but it's a school assignment, so I can't. I know it's annoying and I'm sorry, but using the errors I got in the Terminal I tried to point where the error is. I'm sorry again and thank you for your time.

Mat
  • 202,337
  • 40
  • 393
  • 406

2 Answers2

1

Your function call is wrong. As warnings are self explanatory that, check expects argument of type char **. **arr (dereferencing double pointer arr) is of type char not char **.
Call it as

check(arr);
Community
  • 1
  • 1
haccks
  • 104,019
  • 25
  • 176
  • 264
  • Wow, thanks, that clears up a lot of errors except the last one at line 57. Does that mean I have to type "check(char **arr) {" instead of "check(**arr) {"? – VarimathrasXX Dec 21 '13 at 17:23
  • 1
    @VarimathrasXX Declare the param as `char **arr`, as in `char check(char **arr)` However, I can already tell you you're going to need to pass the size info in as well, `char check(char **arr, int rows, int cols)` – WhozCraig Dec 21 '13 at 17:23
1

You already declare arr as a pointer. So when you call it as **arr, it's dereferenced back to a char. Calling it just as check(arr), etc, probably solves your problem.

gnometorule
  • 2,151
  • 2
  • 20
  • 29