pointer to matrix syntax 
Author Message
 pointer to matrix syntax

Hi,

    If I have an array declared as

    float x[4][4];

    and I want to pass a pointer to it into a function like so

    doit (&x);

    What should the function prototype look like? I tried

    void doit (float *[][4]);

    but the compiler issues a warning saying:

cc memcpy_test.c
cc-1164 cc: WARNING File = memcpy_test.c, Line = 28
  Argument of type "float (*)[4][4]" is incompatible with parameter of type
          "float (*)[4]".

     doit (&x);
              ^

I realize I could just say doit (x), and have the function prototype look
like
void doit(float[][4]); and it would work, but I don't want to do that for
other
reasons.

- pete



Tue, 21 Sep 2004 22:48:51 GMT  
 pointer to matrix syntax

Quote:
> Hi,
>     If I have an array declared as
>     float x[4][4];
>     and I want to pass a pointer to it into a function like so
>     doit (&x);
>     What should the function prototype look like? I tried
>     void doit (float *[][4]);
>     but the compiler issues a warning saying:
> cc memcpy_test.c
> cc-1164 cc: WARNING File = memcpy_test.c, Line = 28
>   Argument of type "float (*)[4][4]" is incompatible with parameter of type
>           "float (*)[4]".
>      doit (&x);
>               ^
> I realize I could just say doit (x), and have the function prototype look
> like
> void doit(float[][4]); and it would work, but I don't want to do that for
> other
> reasons.

You want a pointer to an array of 4 arrays of 4 floats, i.e.
void doit(float (*)[4][4]);
/* ... code here ... */
void doit(float (*parameter)[4][4]) {
  /* ... code here ... */

Quote:
}

--

| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste       W++ B OP+                     |
\----------------------------------------- Finland rules! ------------/
"To know me IS to love me."
   - JIPsoft


Tue, 21 Sep 2004 22:52:59 GMT  
 pointer to matrix syntax

Quote:

>     If I have an array declared as
>     float x[4][4];
>     and I want to pass a pointer to it into a function like so
>     doit (&x);
>     What should the function prototype look like? I tried
>     void doit (float *[][4]);

/* doit2 form not available in C89 | C90 */

void doit1(float (*y)[][4]);
void doit2(int n, int m, float (*y)[n][m]);

int main(void)
{
    float x[4][4];
    doit1(&x);
    doit2(4, 4, &x);
    return 0;

Quote:
}

void doit1(float (*y)[][4])
{

Quote:
}

void doit2(int n, int m, float (*y)[n][m])
{

Quote:
}

--
It is better that the grammarians should chide us
than that the people should not understand us.
  - St. Augustine


Wed, 22 Sep 2004 05:46:03 GMT  
 pointer to matrix syntax

Quote:

>           "float (*)[4]".

That's the answer.

/* BEGIN new.c */

#include <stdio.h>

void doit(float (*x)[4])
{
    size_t a, b;

    a = 3;
    do {
        b = 3;
        do {
            x[a][b] = a + b + 0.5f;
        } while (b--);
    } while (a--);

Quote:
}

int main(void)
{
    float x[4][4];
    size_t a, b;

    doit(x);
    a = 3;
    do {
        b = 3;
        do {
            printf("%f ", x[a][b]);
        } while (b--);
        puts("");
    } while (a--);
    return 0;

Quote:
}

/* END new.c */

--
 pete



Wed, 22 Sep 2004 16:54:42 GMT  
 pointer to matrix syntax
On Friday, in article


Quote:
>Hi,

>    If I have an array declared as

>    float x[4][4];

>    and I want to pass a pointer to it into a function like so

>    doit (&x);

Is there any particular reason for this? It is simpler all round to pass
it like this

     doit (x);

That passes a pointer to the first element of the array (i.e. a pointer
to an array of 4 floats) which is the normal way to reference an array.
For example if you have

     char str[100];

you would normally pass a pointer using

     func(str);

not
     func(&str);

Arrays of arrays are no different in this respect.

Quote:
>    What should the function prototype look like? I tried

>    void doit (float *[][4]);

To do it the way you want (i.e. called with doit(&x) ) it would have to be

     void doit (float (*)[4][4]);

or possibly

     void doit (float (*)[][4]);

If you call with doit(x) then you would use

     void doit (float [][4]);

or

     void doit (float (*)[4]);

Quote:
>    but the compiler issues a warning saying:

>cc memcpy_test.c
>cc-1164 cc: WARNING File = memcpy_test.c, Line = 28
>  Argument of type "float (*)[4][4]" is incompatible with parameter of type

The compiler showed you the correct form above.

Quote:
>          "float (*)[4]".

>     doit (&x);
>              ^

>I realize I could just say doit (x), and have the function prototype look
>like
>void doit(float[][4]); and it would work, but I don't want to do that for
>other
>reasons.

What other reasons?

--
-----------------------------------------


-----------------------------------------



Wed, 22 Sep 2004 20:43:54 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. add and multiply matrices with pointer Arithmetic help.

2. Pointers and matrices

3. Q: Matrixes and pointers

4. Pointers & matrices: Twist on the original question

5. problem with malloc and pointer matrixes

6. HELP with integer pointer matrix

7. Matrix, CObject and pointers

8. How to return pointers to matrices

9. matrix-matrix multiplication in ANSI C

10. Matrix inverse and matrix multiplication

11. pointer syntax

12. syntax for max array of function pointers?

 

 
Powered by phpBB® Forum Software