J
Jess
Hello,
I understand that if an array is illegal in some particular context,
then its name will be converted to a pointer. For example, if
int a[] = {1,2,3};
then a function
void f(int* a);
will be same as
void f(int a[]);
However, the question is if I have a multi-dimensional array
int b[][3] = {{1,2,3},{4,5,6}};
I think to pass "b" to a function "g", the signature of "g" should be
something like
template<size_t n>
void g(int (*b)[n]);
I tried this, and it worked. I also tried
void g(int** b);
and failed. This suggests that when "b" is a 2D array, its name is a
pointer pointing to an integer array, is this the reason why "int**"
failed?
However, the signature of "main()" function is
int main(int argc, char** argv)
or
int main(int argc, char (*argv)[]);
Why can we use either char** or char (*argv)[] in this case?
Moreover, if a function like "g" above accepts arrays of any size, is
the templatized version the only solution? I tried
void g(int (*b)[]);
and failed.
Thanks a lot,
Jess
I understand that if an array is illegal in some particular context,
then its name will be converted to a pointer. For example, if
int a[] = {1,2,3};
then a function
void f(int* a);
will be same as
void f(int a[]);
However, the question is if I have a multi-dimensional array
int b[][3] = {{1,2,3},{4,5,6}};
I think to pass "b" to a function "g", the signature of "g" should be
something like
template<size_t n>
void g(int (*b)[n]);
I tried this, and it worked. I also tried
void g(int** b);
and failed. This suggests that when "b" is a 2D array, its name is a
pointer pointing to an integer array, is this the reason why "int**"
failed?
However, the signature of "main()" function is
int main(int argc, char** argv)
or
int main(int argc, char (*argv)[]);
Why can we use either char** or char (*argv)[] in this case?
Moreover, if a function like "g" above accepts arrays of any size, is
the templatized version the only solution? I tried
void g(int (*b)[]);
and failed.
Thanks a lot,
Jess