Array parameter with size

How did it happen, even if I give the full dimensions, the size of the array is just one of the pointers? Is it fading too?

#include <iostream>
using namespace std;

void function(char* arr[1][2]){
    cout << sizeof(arr); // 4
}

int main() {
    char* params2d[1][2] = { {"hello", "world"} };
    cout << sizeof(params2d); // 8
    function(params2d);
    return 0;
}
+4
source share
3 answers

All function declarations

void function(char* arr[1][2]);
void function(char* arr[][2]);
void function(char* (*arr)[2]); 

are the same. In the case of multidimensional arrays, the first dimension can be omitted. So do you write

void function(char* arr[1][2]);  

or

void function(char* arr[10][2]);  

converts it to

void function(char* (*arr)[2]);
+3
source

Yes, it is fading. An argument to a function of type "array of X" is just syntactic sugar for a "pointer to X". If the parameter type is not an “array reference”, the array used as an argument for this parameter is always converted to a pointer.

+1

, :

void function(char* (&arr)[1][2])

:

void function(char* (*arr)[2]);
+1

Source: https://habr.com/ru/post/1544479/


All Articles