Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initializing array with two pointers

Tags:

arrays

c

int main() {
    int **matrix = {
        {1, 3, 2, 4},
        {3, 2, 4, 5},
        {9, 3, 2, 1}
    };

    getchar();
}
  1. Why does this display warnings like "braces around scalar initializer"?
  2. Why do I need to initialize multidimentional arrays with more than one pointers? (if you could give me some pretty easy-to-understand explanation on this one...)
  3. If I'd want to use int matrix[3][4] instead of int **matrix...what would be a function parameter if I'd want to pass this array? int[][]?
like image 220
khernik Avatar asked Dec 15 '25 06:12

khernik


1 Answers

int ** is a pointer type not an array type. Pointers are not arrays. Use type int [3][4].

You cannot pass arrays to functions but you can pass a pointer to an array. A function declaration to pass a pointer to an array 4 of int would be:

void f(int arr[3][4]);

or

void f(int arr[][4]);

or

void f(int (*arr)[4]);

The three declarations are equivalent.

like image 172
ouah Avatar answered Dec 16 '25 22:12

ouah



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!