469,898 Members | 1,733 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,898 developers. It's quick & easy.

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 2811
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by terence.parker | last post: by
reply views Thread by Sebastian Hiller | last post: by
1 post views Thread by Waqarahmed | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.