473,386 Members | 1,609 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

names of multi-dimensional arrays

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
2 1429
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Monty | last post by:
Lets say you've created a css style called .ninetypercent that sets the font size to 90%. Then lets say you want that style to be applies to certain types of textual content (e.g., help text and...
10
by: ynott | last post by:
I have an Access database with 58 fields in one table. I wrote many of the field names so that they were descriptive so that others could figure it out in the future. As an example, one field...
19
by: Barman Brakjoller | last post by:
Just curious, what techniques does gmail leverage to hide the javascript code that their service uses to work? I've tried View Source here and there in the page without much luck. I have no...
15
by: phil-news-nospam | last post by:
This question is borderline between language and programming, but I want to focus more on the language standards issue, rather than the programming issue, so I am posting here. I have a number...
5
by: bobwansink | last post by:
Hi, I'm relatively new to programming and I would like to create a C++ multi user program. It's for a project for school. This means I will have to write a paper about the theory too. Does anyone...
2
by: Arnold | last post by:
Greetings Gurus, In a report showing the names of students and their progress, I am getting an error in the name field (Name: #Error). The report gets its data from an unbound form containing...
3
by: Chris Sharman | last post by:
Are spaces allowed in names ? Eg <input name="my field" type="text" value="my data"> The html4 dtd seems to say this is cdata, which allows embedded single spaces, but say agents may trim...
1
by: Alf P. Steinbach | last post by:
* Adam, in clc++m: > I have an unfortunate case where a single class wants to derive from two > existing classes: > > struct A { virtual long fun() = 0; }; > struct B { virtual bool fun() =...
6
by: Mike S | last post by:
Hi all, A (possibly dumb) question, but I've had no luck finding a definitive answer to it. Suppose I have two tables, Employees and Employers, which both have a column named "Id": Employees...
3
by: cjb | last post by:
Is there a way to get Directory.GetFiles to return multi-language file names? I haven't found any overloads that allow any such parameter. The way I am using it now is: foreach (string d in...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.