473,385 Members | 1,912 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,385 software developers and data experts.

Another Question

I'm working on my homework for C++, and have a problem. I have a
multi-dimension array, Array1[5][4] that I can't define in the function
declaration and function definition. I keep getting an error. I've tried
the following:

void Sum ( int [][] );

I get an error with this, and also with:

void Sum ( int [] );

The compiler, Microsoft VC6 can't convert the two-dimensional array to a
one-dimension array.

What I'm I doing wrong? I can't find the answer in my notes or the book,
they all deal with one-dimension arrays.

John Howard
Jul 19 '05 #1
3 3122
John Howard wrote:
I'm working on my homework for C++, and have a problem. I have a
multi-dimension array, Array1[5][4] that I can't define in the function
declaration and function definition. I keep getting an error. I've tried
the following:

void Sum ( int [][] );

I get an error with this, and also with:

void Sum ( int [] );

The compiler, Microsoft VC6 can't convert the two-dimensional array to a
one-dimension array.

What I'm I doing wrong? I can't find the answer in my notes or the book,
they all deal with one-dimension arrays.


The array declaration

int a[3][2]

declares an array variable 'a' that has 3 elements, where each element
'a[n]' is an object of type 'int[2]':

typeof(a[n]) == int[2]

IOW, each element in 'a' is an "array object" consisting of 2 int
subobjects:

a[0] ::= [[int][int]] // int[2] object
a[1] ::= [[int][int]] // int[2] object
a[2] ::= [[int][int]] // int[2] object

Recall that when you pass an array to (or return an array from) a
function, what you are really doing is passing the address of the
array's first element:

void g1 (int *q, int size);
void g2 (int q[], int size); // alternate "array argument" syntax

int main() {
int b[5];

g1(b, 5); // i.e., g1(&b[0], 5);
g2(b, 5); // i.e., g2(&b[0], 5);

/* n.b.
* typeof(b[0]) == int
* typeof(&b[0]) == int*
*/
}

Since 'a[0]' is an object of type 'int[2]', the address of a's first
element (i.e., '&a[0]') will be a pointer whose type is 'int(*)[2]':

typeof(a[0]) == int[2]
typeof(&a[0]) == int(*)[2]

So to pass the address of array a's first element (i.e., &a[0]) into a
function, you need to declare the function's arguments accordingly:

<example>
// n.b. In functions f1 and f2 below, the formal parameter 'p'
// is a pointer to an 'int[2]' object.

void f1 (int(*p)[2], int rows)
{
for (int r = 0; r < rows; ++r) {
for (int c = 0; c < 2; ++c) {
p[r][c] = 0;
}
}
}

// or, using the alternate "array argument" syntax,

void f2 (int p[][2], int rows) { /* ... */ }

int main()
{
int a[3][2];
f1 (a, 3);
f2 (a, 3);
}
</example>

n.b. Applying the array indexing operator '[]' to an int[2] object lets
you access the individual int subobjects within the int[2] object:

typeof(p[r]) == int[2]
typeof(p[r][c]) == int

e.g.

p[r] ::= [[567][222]]
p[r][0] ::= 567
p[r][1] ::= 222

FWIW, these concepts carry over to all N-dimension arrays:

void d1 (int (*parray)[4][3], int dim1);
void d2 (int parray[][4][3], int dim1); // alternate syntax

int c[5][4][3];

d1 (c, 5); // i.e., d1(&c[0], 5);
d2 (c, 5); // i.e., d2(&c[0], 5);

typeof(c) == int[5][4][3]
typeof(&c) == int(*)[5][4][3]

typeof(c[i]) == int[4][3]
typeof(&c[i]) == int(*)[4][3]

typeof(c[i][j]) == int[3]
typeof(&c[i][j]) == int(*)[3]

typeof(c[i][j][k]) == int
typeof(&c[i][j][k]) == int(*) == int*

--
Jim

To reply by email, remove "link" and change "now.here" to "yahoo"
jfischer_link5809{at}now.here.com
Jul 19 '05 #2
hi,
i would do it quick and dirty, if everything else fails:
void Sum(void* lpBlabla)// prototype
cast it back to int[][] in function Sum

call it so:
Sum((void*)&Sum[0][0]);


-------- Original Message --------
Subject: Another Question (18-Jul-2003 17:45)
From: John Howard <jd********@yahoo.com>
To: comp.lang.c++
I'm working on my homework for C++, and have a problem. I have a
multi-dimension array, Array1[5][4] that I can't define in the function
declaration and function definition. I keep getting an error. I've tried
the following:

void Sum ( int [][] );

I get an error with this, and also with:

void Sum ( int [] );

The compiler, Microsoft VC6 can't convert the two-dimensional array to a
one-dimension array.

What I'm I doing wrong? I can't find the answer in my notes or the book,
they all deal with one-dimension arrays.

John Howard


Jul 19 '05 #3


Administratoren wrote:

hi,
i would do it quick and dirty, if everything else fails:
void Sum(void* lpBlabla)// prototype
cast it back to int[][] in function Sum

call it so:
Sum((void*)&Sum[0][0]);


That's the worst advise one could give.
Please refrain from giving such advises in
the future.

Thank you.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: fig000 | last post by:
Hi, I'm relatively new to Javascript so please bear with me on what might sound like silly questions. This is what I want to do: I'm working in classic asp (I have to for this project). I...
6
by: anon | last post by:
Post Forwarding question...... For this control below, <asp:Button runat="server" PostTargetUrl="page2.aspx" /> The Attribute: PostTargetUrl="page2.aspx" Is this PostTargetUrl Attribute...
2
by: terence.parker | last post by:
I am often faced with the dilemma of whether to use a JOIN query across three tables in order to grab a bunch of results - or whether to create another table to represent what I want. The latter is...
0
by: Sebastian Hiller | last post by:
Hello, i'm new to .Net (i'm using VB as language and i'm working in the code-behind mode) and i can't solve the following problem: I have a WebForm and want to Add a UserControl...
3
by: Brad | last post by:
I have another hopefully simple question. I am so used to writing VB .Net windows apps that there are some things in ASP .Net that just does not easily cross over. I know how to pass variables to...
27
by: Javier Martinez | last post by:
Hi I have asp application in a machine with a virtual directory referring a shared directory in another machine When I try to load any aspx page of my portal I get the following error: ...
17
by: Eric_Dexter | last post by:
def simplecsdtoorc(filename): file = open(filename,"r") alllines = file.read_until("</CsInstruments>") pattern1 = re.compile("</") orcfilename = filename + "orc" for line in alllines: if not...
24
by: David Mathog | last post by:
On a Solaris 8 system if a user "joe" logs in, for instance via ssh, cuserid() returns "joe". That's the expected behavior and so far so good. However if that user then does: % su - sally ...
13
by: shookim | last post by:
I don't care how one suggests I do it, but I've been searching for days on how to implement this concept. I'm trying to use some kind of grid control (doesn't have to be a grid control, whatever...
4
by: MichaelK | last post by:
Hello. I have all data already collected on the current page? I want to open another window with the form, fill the fields and submit that form. So basically the question is how can I fill all...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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.