passing 2d arrays by reference 
Author Message
 passing 2d arrays by reference

i have some problems passing a 2d array of int's, by reference, to a
subprogram.
if i just pass a normal array of int's it works fine, but when i add
on another set of square brackets, everything goes to hell :)

the following code gives me the error.. "arithmetic on pointer to an
incomplete type", on the line calc() where i assign stuff to point[][]

void calc(int point[][]) {
    int i;
    for (i=0;i<2;i++)
        point[i][i] = (3 * point[i][i]);

Quote:
}

int main() {
    int point[3][3];
    calc(point);

Quote:
}

i know this doesnt do anything useful but its just an example.
any help/abuse is greatly appreciated.

-Josh

--



Wed, 08 Oct 2003 14:18:08 GMT  
 passing 2d arrays by reference

Quote:
>i have some problems passing a 2d array of int's, by reference, to a
>subprogram.
>if i just pass a normal array of int's it works fine, but when i add
>on another set of square brackets, everything goes to hell :)

>the following code gives me the error.. "arithmetic on pointer to an
>incomplete type", on the line calc() where i assign stuff to point[][]

>void calc(int point[][]) {

You have to declare all but the first dimension. In your case, you must write:

void calc(int point[][3])

And no, there is no way to generalize this, i.e. to write something like:
void calc(int dim2, int point[][dim2])
--



Thu, 09 Oct 2003 04:15:23 GMT  
 passing 2d arrays by reference
try this:

void calc(void *point)
...
calc(*point)


Quote:
> i have some problems passing a 2d array of int's, by reference, to a
> subprogram.
> if i just pass a normal array of int's it works fine, but when i add
> on another set of square brackets, everything goes to hell :)

> the following code gives me the error.. "arithmetic on pointer to an
> incomplete type", on the line calc() where i assign stuff to point[][]

> void calc(int point[][]) {
>     int i;
>     for (i=0;i<2;i++)
>         point[i][i] = (3 * point[i][i]);
> }

> int main() {
>     int point[3][3];
>     calc(point);
> }

> i know this doesnt do anything useful but its just an example.
> any help/abuse is greatly appreciated.

> -Josh

> --


--



Thu, 09 Oct 2003 04:25:07 GMT  
 passing 2d arrays by reference
See annotation


Quote:
>i have some problems passing a 2d array of int's, by reference, to a
>subprogram.
>if i just pass a normal array of int's it works fine, but when i add
>on another set of square brackets, everything goes to hell :)

>the following code gives me the error.. "arithmetic on pointer to an
>incomplete type", on the line calc() where i assign stuff to point[][]

>void calc(int point[][]) {

You cannot declare the parameter this way.  Only the highest level
subscript may be omitted from the brackets.  In this case you need
        void calc(int point[][3]){

Quote:
>    int i;
>    for (i=0;i<2;i++)
>        point[i][i] = (3 * point[i][i]);
>}

>int main() {
>    int point[3][3];
>    calc(point);
>}

>i know this doesnt do anything useful but its just an example.
>any help/abuse is greatly appreciated.

>-Josh


<<Remove the del for email>>
--



Thu, 09 Oct 2003 04:26:47 GMT  
 passing 2d arrays by reference


Quote:
>try this:

>void calc(void *point)
>...
>calc(*point)



>> i have some problems passing a 2d array of int's, by reference, to a
>> subprogram.
>> if i just pass a normal array of int's it works fine, but when i add
>> on another set of square brackets, everything goes to hell :)

>> the following code gives me the error.. "arithmetic on pointer to an
>> incomplete type", on the line calc() where i assign stuff to point[][]

>> void calc(int point[][]) {
>>     int i;
>>     for (i=0;i<2;i++)
>>         point[i][i] = (3 * point[i][i]);
>> }

>> int main() {
>>     int point[3][3];
>>     calc(point);
>> }

>> i know this doesnt do anything useful but its just an example.
>> any help/abuse is greatly appreciated.

>> -Josh

>> --


While your suggestion itself does not cause a syntax error, it is
useless unless you show him how he has to modify his code to allow
references to the array elements.  Once you see the number and
ugliness of the changes, it is easier to change the parameter list so
it is correct to start with.

<<Remove the del for email>>
--



Fri, 10 Oct 2003 03:44:27 GMT  
 passing 2d arrays by reference

Quote:

> i have some problems passing a 2d array of int's, by reference, to a
> subprogram.
> if i just pass a normal array of int's it works fine, but when i add
> on another set of square brackets, everything goes to hell :)

That's because there is not really such a thing as a 2D array, in C.
There are just arrays of arrays. Only the outermost array (the first
pair of []'s) may be unknown.

Quote:
> the following code gives me the error.. "arithmetic on pointer to an
> incomplete type", on the line calc() where i assign stuff to point[][]

It has to: it doesn't know how many elements there are in one 'row' of
your matrix, so it can't know where the second row would start.

Quote:
> void calc(int point[][]) {
>     int i;
>     for (i=0;i<2;i++)
>         point[i][i] = (3 * point[i][i]);
> }

To make this work, you'ld have to make this

        void calc (int point[][3]) {
          /* rest can stay as is */
        }

But this means you can only pass any array of dimension (something)x3
to this function. For fully dynamically-sized matrix operations, you
either need the "variable length array" extension of the C99 version
of Standard C, or resort to pointers and the allocation method shown
in the C FAQ.

--

Even if all the snow were burnt, ashes would remain.
--



Sat, 11 Oct 2003 15:27:29 GMT  
 passing 2d arrays by reference

writes

Quote:
>i have some problems passing a 2d array of int's, by reference, to a
>subprogram.
>if i just pass a normal array of int's it works fine, but when i add
>on another set of square brackets, everything goes to hell :)

And that is the way it will be. Only the top level array decays to a
pointer, and that pointer must have a fully defined type so you can
write:

void f(int a[][3]); // a is an array of arrays of 3 ints

but

void f(int a[][]);
is an error because a is an array of an unspecified type.

Francis Glassborow      ACCU
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
--



Sat, 11 Oct 2003 15:30:02 GMT  
 passing 2d arrays by reference

Quote:
> void calc(int point[][]) {
>     int i; for (i=0;i<2;i++)
>         point[i][i] = (3 * point[i][i]);
> }
> int main() {
>     int point[3][3]; calc(point);
> }

C implements 2D arrays as arrays of arrays, stuffed in to one contiguous block
of memory.

If you have a n*10 array, to access element 3,4, it is accessed something
like this:

*(array+ 10*4 + 3)

As you can see, the size (10) is needed. If you redeclare cals as

void calc(int point[][3])

It should work. I personally don't like this much, since you can't pass
2D arrays of arbitraty dimensions.

If you creat a 2D array like this (an array of pointers pointing to
integers) but rather better coded:

int **array;

array=calloc(xsize, sizeof(int*));

for(i=0;i<xsize;i++)
        array[i]=calloc(ysize, sizeof(int);

You could then declare calc as

void calc(int **point)

The useful thing about making 2D arrays using that method is that they
can be used as a direct replacement for 2D arrays because you can still
access them with [a][b].

-Ed

--
You can't go wrong with psycho-rats.

u 9 8 e j r (at) e c s . o x . a c . u k
--



Mon, 13 Oct 2003 20:46:07 GMT  
 passing 2d arrays by reference

....
Quote:
> You have to declare all but the first dimension. In your case, you must write:

> void calc(int point[][3])

Correct; or int (*point)[3] which is equivalent
ONLY for a function parameter.

Quote:
> And no, there is no way to generalize this, i.e. to write something like:
> void calc(int dim2, int point[][dim2])

Not in the past, but C99 adds this feature, called a
variable length array (VLA) (also for non-parameters)
and it works now in GNU C (i.e. gcc in nonstandard mode).
GNU C also allows you to use a parameter that occurs later
in the parameter sequence to set VLA bounds; C99 doesn't.

--
- David.Thompson 1 now at worldnet.att.net
--



Wed, 15 Oct 2003 19:23:15 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. pass allocated 2d array by reference

2. passing 2d arrays by reference

3. How To pass 2D String array to VB from VC++ Using Safe array

4. Which 2D array of 2D array addressing method?

5. 2D array of pointers to 2D arrays

6. 2D array reference

7. Passing a 2d array from .NET to COM

8. Passing 2D arrays to functions with const.

9. Passing 2D arrays to function

10. Passing 2D Arrays to functions

11. 2d array passing

12. Passing 2D Array from Fortran 90 to C

 

 
Powered by phpBB® Forum Software