Pointers & matrices: Twist on the original question 
Author Message
 Pointers & matrices: Twist on the original question

I have a structure which has a multidimensional array as a member. I want to
set up a pointer to that member so that I can refer to the the
multidimensional array in a shorted way.

struct S {
  double A[10][20];

Quote:
};

I can refer to elements in the array as S.A[i][j], but I would like to set
up a pointer to the array, say P, so that I can refer to the array as
P[i][j]. This is straightforward for one-dimensional arrays, but I haven't
found the magic to allow P to do the same for multidimensional arrays.


Thu, 02 Sep 2004 21:25:32 GMT  
 Pointers & matrices: Twist on the original question

Quote:
> I have a structure which has a multidimensional array as a member. I want
to
> set up a pointer to that member so that I can refer to the the
> multidimensional array in a shorted way.

> struct S {
>   double A[10][20];
> };

> I can refer to elements in the array as S.A[i][j], but I would like to set
> up a pointer to the array, say P, so that I can refer to the array as
> P[i][j]. This is straightforward for one-dimensional arrays, but I haven't
> found the magic to allow P to do the same for multidimensional arrays.

If you're absolutely, definitively, positively sure about that magic
number '20', the following will do:

    typedef double (*row_t)[20];

    struct S Foo;
    row_t P= Foo.A;

    /* fiddle diddle with P[i][j] */

kind regards,




Thu, 02 Sep 2004 22:40:02 GMT  
 Pointers & matrices: Twist on the original question

Quote:


> > I have a structure which has a multidimensional array as a member. I want
> to
> > set up a pointer to that member so that I can refer to the the
> > multidimensional array in a shorted way.

> > struct S {
> >   double A[10][20];
> > };

> > I can refer to elements in the array as S.A[i][j], but I would like to set
> > up a pointer to the array, say P, so that I can refer to the array as
> > P[i][j]. This is straightforward for one-dimensional arrays, but I haven't
> > found the magic to allow P to do the same for multidimensional arrays.

> If you're absolutely, definitively, positively sure about that magic
> number '20', the following will do:

>     typedef double (*row_t)[20];

>     struct S Foo;
>     row_t P= Foo.A;

>     /* fiddle diddle with P[i][j] */

Actually, if using the typedef way, go all the way and
use the same one in the struct too, thus ensuring the a, d & p.

typedef double rowT[20];
struct S {
    rowT a[10];

Quote:
};

int main(void) {
    struct S foo;
    rowT *p = foo.a;

    p[1][2] = 1.3;
    return 0;

Quote:
}

Alternatively, don't use a typedef and just declare it like:

struct S {
    double a[10][20];

Quote:
};

int main(void) {
    struct S foo;
    double (*p)[20] = foo.a;

    p[1][2] = 1.3;
    return 0;

Quote:
}

The first method is better, as it centralizes the magic number
20. This second one included to show the declarations.

--
/Svante

http://axcrypt.sourceforge.net
Free AES Point'n'Click File Encryption for Windows 9x/ME/2K/XP



Thu, 02 Sep 2004 23:24:10 GMT  
 Pointers & matrices: Twist on the original question


Quote:
>I have a structure which has a multidimensional array as a member. I want to
>set up a pointer to that member so that I can refer to the the
>multidimensional array in a shorted way.

>struct S {
>  double A[10][20];
>};

>I can refer to elements in the array as S.A[i][j], but I would like to set

No you can't.  S is the tag for the struct, not the name of an object
of type struct S.  However, if you change the declaration to a
definition like the one below then you can:
        struct S {...} S;
Quote:
>up a pointer to the array, say P, so that I can refer to the array as
>P[i][j]. This is straightforward for one-dimensional arrays, but I haven't
>found the magic to allow P to do the same for multidimensional arrays.

For a two-dimensional array declared like A, you could use
        double (*P)[20] = S.A;
which says P is a pointer to an array of 20 double (and initialized to
the address of the first element of S.A).

Remember that in c multidimensional arrays are actually arrays of
arrays so A is really an array of 10 (array of 20 double).  The first
element of A is A[0] (not A[0][0]) and A[0] is simply the first such
array of 20 double.  The expression S.A is treated by the compiler in
this context as the address of the first element of S.A (the address
of the first array of 20 double) which is exactly the type of address
P is expecting.

As with any pointer, P[0] is a dereference of P to the first object it
points to, in this case an array of 20 double.  So P[0][0] would be
the first double in that array.  From here, it should be easy to see
that P[i][j] and S.A[i][j] refer to the same element of S.A.

<<Remove the del for email>>



Fri, 03 Sep 2004 00:40:08 GMT  
 Pointers & matrices: Twist on the original question


<...>

Quote:
> The first method is better, as it centralizes the magic number
> 20. This second one included to show the declarations.

Excellent. I like your approach. I'am reading you for a while, and I
really appriciate your posts.

--
-ed- emdel at noos.fr
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-library: http://www.dinkumware.com/htm_cl/index.html
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/



Fri, 03 Sep 2004 02:18:59 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Serializing a pointer array with a twist

2. Adjacency Matrix & General Questions

3. Seven Original Sins of K&R (Long)

4. original pos & size of a component

5. C Question - pointer & address-operator

6. Question about character arrays & pointers

7. Question RE: Arrays, Rand, & Pointers

8. SIMPLE malloc & pointer question

9. Pointer & Dialog question

10. DOS, pointers & flat addressing Question

11. Difference between pointers & arrays - A question

12. Pointer&Dialog question

 

 
Powered by phpBB® Forum Software