names of multi-dimensional arrays

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
 
C

contactmayankjain

Hi 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);

I think ** is used to point to a pointer so it doesn't work and its
not like a pointer to a 2 dimensional array.
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)

here argv is a pointer to a pointer so we use main like main(int argc,
char* args[])
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

Regards
Mayank Jain
Niksun
9818390836
www.mayankjain.110mb.com
 
J

James Kanze

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?

The key to understanding this is the realize that C++ doesn't
have 2D arrays, at least not in the mathematical sense. It uses
arrays of arrays. The type of your b is "array[2] of array[3]
of int". This is the type which gets converted to a pointer:
"pointer to array[3] of int". And of course, a "pointer to
array[3] of int" is not an array, so the array to pointer
conversion doesn't apply to it.
However, the signature of "main()" function is
int main(int argc, char** argv)
or
int main(int argc, char (*argv)[]);

Stop. That second declaration is *NOT* a standard signature of
main(). The standard signature of main would be:
int main( int argc, char *argv[] ) ;
that is:
int main( int argc, char *(argv[]) ) ;

Formally, you write the second argument as "array[] of pointer
to char". Since it's an array, it becomes "pointer to pointer
to char".
Why can we use either char** or char (*argv)[] in this case?

You can't. You can use either char** or char *argv[].
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.

Yup. A pointer to an array must point to an array of a known,
constant dimension.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top