By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,278 Members | 1,136 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,278 IT Pros & Developers. It's quick & easy.

names of multi-dimensional arrays

P: n/a
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

Jul 7 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Hi Jess,

Jess wrote:
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

Jul 7 '07 #2

P: n/a
On Jul 7, 3:05 pm, Jess <w...@hotmail.comwrote:
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.

--
James Kanze (Gabi Software) email: ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jul 7 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.