"kohvirus@gmail.com" <kohvirus@gmail.com> writes:[color=blue]
> Hello everyone. I'm currently trying to write a program with an array
> but for the life of me I can't seem to get the right syntax for the
> array. Here is the program as follows:
>
> #include <iostream>
> #include <stdio.h>
>
>
> void display(int A[] [], int m, int n)
>
> {
> int i;
> int j;
>
> std::cout << "Enter your value for m ";
> std::cin >> m;
> std::cout << "\n";
>
> for (i =0; i< m; i++)
>
> { for (j=0; j < n; j++)
>
> cout << " " << A[i][j];
>
> cout << "\n";
> }
>
> }[/color]
First of all, your code is C++, not C. If you have questions about
C++, comp.lang.c++ is down the hall on the left.
But as it happens, the array issues are (I'm fairly sure) common to C
and C++.
What looks like an array parameter declaration really declares a
pointer parameter. For example,
void func(int param[]);
really means
void func(int *param);
For that matter, so does this:
void func(int param[10]);
The 10 is simply ignored.
[color=blue]
> Sorry if the way I write makes you cringe, still learning to to make
> code 'neat'. Anyway, here are the error outputs I am getting and I am
> unable to resolve any further:
>
> fail.cpp:9: declaration of `A' as multidimensional array
> fail.cpp:9: must have bounds for all dimensions except the first
> fail.cpp: In function `void display(int, int)':
> fail.cpp:21: `A' undeclared (first use this function)
> fail.cpp:21: (Each undeclared identifier is reported only once
> fail.cpp:21: for each function it appears in.)[/color]
As the compiler is telling you, you need to specify the bounds of
all dimensions except the first. So this:
void func(int a[][10]);
is legal, and really means
void func(int (*a)[10]);
but this
void func(int a[][]);
is is illegal.
There really isn't any simple way to declare a two-dimensional array
(equivalently, an array of arrays) with both bounds allowed to vary.
Array indexing is defined in terms of pointer arithmetic, which
requires a fixed element size, so you have to decide in advance how
long each row of your array needs to be.
There are several approaches that give you more flexibility. One of
the most common is to use a pointer-to-pointer:
void func(int **a);
The argument can be a pointer to an array of pointers, where element
in turn points to an array of int. You have to explicitly allocate
(probably using malloc()) the array of pointers *and* each row of
ints, which means you have to explicitly deallocate everything when
you're done.
If you want to take this approach, section 6 of the C FAQ (google it
if you don't know where it is) has a lot of good information -- as do
all the other sections.
<OT>
On the other hand, if you're really using C++, there's probably
something in the STL that will do most of the work for you -- but we
can't discuss it here.
</OT>
--
Keith Thompson (The_Other_Keith)
kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.