0

I'm very new to programming so I'm sorry if this will sound as a bad question.

My Code:

#include <stdio.h>

void Order(int *waiterList){

    printf("Number is %d", *waiterList[0][1]);

}

int main(){

    int waiterList[3][3] = {{1,2,3},{4,5,6},{7,8,9}};

    Order(&waiterList);

    return 0;
}

This code is giving me this error:

pointer.c: In function 'Order':
pointer.c:5:39: error: subscripted value is neither array nor pointer nor vector
printf("Number is %d", *waiterList[0][1]);

pointer.c: In function '`enter code here`main':
pointer.c:13:8: warning: passing argument 1 of 'Order' from incompatible pointer type [- 
Wincompatible-pointer-types]
  Order(&waiterList);

pointer.c:3:6: note: expected 'int *' but argument is of type 'int (*)[3][3]'
void Order(int *waiterList){

Sorry I've never used array before but our final project is requiring us to use array and I'm finding it difficult to understand the articles I found on google... Hope you could help a student out, It's also my first time posting here because I'm kinda desperate.

4
  • Surprise: &waiterList is not an int *. Commented Dec 2, 2019 at 15:06
  • Do you know how I could fix this? I would really appreciate it. Thank you! Commented Dec 2, 2019 at 15:08
  • How to pass 2D array (matrix) in a function in C? Commented Dec 2, 2019 at 15:10
  • It's simple: void Order(int waiterList[3][3]). You need not even worry about "array decay", that part is silently and implicitly handled by the compiler. Commented Dec 2, 2019 at 15:12

2 Answers 2

1

The simple way to achieve this would be

#include <stdio.h>

void Order(int waiterList[3][3]){  // receive an array of type `int [3][3]`
    printf("Number is %d", waiterList[0][1]);

}

int main(){

    int waiterList[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
    Order(waiterList);  // just pass the array name

    return 0;
}

If you must use pointers, use proper types:

#include <stdio.h>

void Order(int (*waiterList)[3][3]){   // pointer to an array of type `int[3][3]`

    printf("Number is %d", (*waiterList)[0][1]);

}

int main(){

    int waiterList[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
    Order(&waiterList);  // pass the address of the array

    return 0;
}
Sign up to request clarification or add additional context in comments.

1 Comment

This was really helpful. Thank you very much!
0

Arrays decay to a pointer to its first element, for a generic array a it decays to &a[0].

In the case of an arrays like your waiterList in the main function, when it decays to a pointer to its first element (&waiterList[0]) it will have the type "pointer to an array". More specifically for waiterList the type becomes int (*)[3]. Which has to be the type of the argument for the Order function:

void Order(int (*waiterList)[3]){
    printf("Number is %d", waiterList[0][1]);
}

You call it simply passing the array as any other variable, no pointer or address-of operator needed:

Order(waiterList);

3 Comments

Did you mean (*waiterList)[0][1]?
@SouravGhosh No, as waiterList is a pointer then waiterList[0] dereferences the pointer already (remember it's equal to *(waiterList + 0)). That dereference will give an array which is then indexed the "normal" way.
@SouravGhosh No it's correct, he doesn't use a pointer to array with two dimensions as in your example. But of course there's no need to use array pointer syntax at all here. int waiterList[3][3] is the most readable parameter declaration.

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.