pointer to a member of a struct which is in turn a member of an arrayof structs

Discussion in 'C Programming' started by pete142, Jan 22, 2008.

  1. pete142

    pete142 Guest

    Hi folks --

    I have a 4-long array t[ ] of
    of struct Targets. And a table of
    int * in p[ ]. I need to set up
    the int * entries in p[ ] such
    that each can reference an int
    in any member of the Targets t[ ]
    array. How to do it?

    Much-simplified example:

    1. #include <stdio.h>
    2. int main(int argc, char* argv[ ]) {
    3. int ix;
    4. typedef struct { int num1;
    int num2;
    int num3;
    } Targets;
    5. Targets t[4];
    6.
    7. typedef struct { int *number; } Pointers;
    8. Pointers *ap;
    9. Pointers p[3] = { { &t[?].num1 },
    10. { &t[?].num2 },
    11. { &t[?].num3 } };
    12. ix = 1; // point t[1]
    13. // Need to reference t[ix] thru p[2]
    14. ap = &p[2];
    15. return *ap->number???? which
    16. resolves to t[1].num2 ;
    17. }

    How do I set up the table of pointers in
    p[ ] so as to be able retrieve t[ix].num2,
    where ix can be 0, 1, or 2?

    Thanks!

    -- pete
    pete142, Jan 22, 2008
    #1
    1. Advertising

  2. Re: pointer to a member of a struct which is in turn a member of an array of structs

    pete142 <> writes:

    > Hi folks --
    >
    > I have a 4-long array t[ ] of
    > of struct Targets. And a table of
    > int * in p[ ]. I need to set up
    > the int * entries in p[ ] such
    > that each can reference an int
    > in any member of the Targets t[ ]
    > array. How to do it?
    >
    > Much-simplified example:
    >
    > 1. #include <stdio.h>
    > 2. int main(int argc, char* argv[ ]) {
    > 3. int ix;
    > 4. typedef struct { int num1;
    > int num2;
    > int num3;
    > } Targets;
    > 5. Targets t[4];
    > 6.
    > 7. typedef struct { int *number; } Pointers;
    > 8. Pointers *ap;
    > 9. Pointers p[3] = { { &t[?].num1 },
    > 10. { &t[?].num2 },
    > 11. { &t[?].num3 } };
    > 12. ix = 1; // point t[1]
    > 13. // Need to reference t[ix] thru p[2]
    > 14. ap = &p[2];
    > 15. return *ap->number???? which
    > 16. resolves to t[1].num2 ;
    > 17. }
    >
    > How do I set up the table of pointers in
    > p[ ] so as to be able retrieve t[ix].num2,
    > where ix can be 0, 1, or 2?


    Pointers p[3] = { { &t[0].num1 },
    { &t[1].num2 },
    { &t[2].num3 } };

    seems to answer you question, but since that is obvious, I must have
    missed what you really want.

    --
    Ben.
    Ben Bacarisse, Jan 22, 2008
    #2
    1. Advertising

  3. pete142

    Guest

    Re: pointer to a member of a struct which is in turn a member of anarray of structs

    pete142 wrote:
    > Hi folks --
    >
    > I have a 4-long array t[ ] of
    > of struct Targets. And a table of
    > int * in p[ ]. I need to set up
    > the int * entries in p[ ] such
    > that each can reference an int
    > in any member of the Targets t[ ]
    > array. How to do it?
    >
    > Much-simplified example:
    >
    > 1. #include <stdio.h>
    > 2. int main(int argc, char* argv[ ]) {
    > 3. int ix;
    > 4. typedef struct { int num1;
    > int num2;
    > int num3;
    > } Targets;
    > 5. Targets t[4];
    > 6.
    > 7. typedef struct { int *number; } Pointers;
    > 8. Pointers *ap;
    > 9. Pointers p[3] = { { &t[?].num1 },
    > 10. { &t[?].num2 },
    > 11. { &t[?].num3 } };
    > 12. ix = 1; // point t[1]
    > 13. // Need to reference t[ix] thru p[2]
    > 14. ap = &p[2];
    > 15. return *ap->number???? which
    > 16. resolves to t[1].num2 ;
    > 17. }
    >
    > How do I set up the table of pointers in
    > p[ ] so as to be able retrieve t[ix].num2,
    > where ix can be 0, 1, or 2?


    You cannot do what you want to do. There is no C syntax for that kind
    of thing. C++ has pointers to members, but in C the closest you can
    come to what you want to do is the following:

    #include <stddef.h>

    int main(int argc, char* argv[ ]) {
    int ix;
    typedef struct { int num1;
    int num2;
    int num3;
    } Targets;
    Targets t[4] = { {1,2,3}, {4,5,6}, {7,8,9}, {10,11,12}};
    typedef struct { size_t off; } Pointers;
    Pointers *ap;

    Pointers p[3] = { { offsetof(Targets, num1) },
    { offsetof(Targets, num2) },
    { offsetof(Targets, num3) } };
    ix = 1;
    ap = &p[2];

    return *(int*)((char*)(t+ix) + ap->off);
    }

    Please note: the C++ pointer to member does essentially the same thing
    as the above code, it just simplifies the syntax considerably.
    , Jan 22, 2008
    #3
  4. pete142

    Guest

    Re: pointer to a member of a struct which is in turn a member of anarray of structs

    Ben Bacarisse wrote:
    > pete142 <> writes:
    >
    > > Hi folks --
    > >
    > > I have a 4-long array t[ ] of
    > > of struct Targets. And a table of
    > > int * in p[ ]. I need to set up
    > > the int * entries in p[ ] such
    > > that each can reference an int
    > > in any member of the Targets t[ ]
    > > array. How to do it?
    > >
    > > Much-simplified example:
    > >
    > > 1. #include <stdio.h>
    > > 2. int main(int argc, char* argv[ ]) {
    > > 3. int ix;
    > > 4. typedef struct { int num1;
    > > int num2;
    > > int num3;
    > > } Targets;
    > > 5. Targets t[4];
    > > 6.
    > > 7. typedef struct { int *number; } Pointers;
    > > 8. Pointers *ap;
    > > 9. Pointers p[3] = { { &t[?].num1 },
    > > 10. { &t[?].num2 },
    > > 11. { &t[?].num3 } };
    > > 12. ix = 1; // point t[1]
    > > 13. // Need to reference t[ix] thru p[2]
    > > 14. ap = &p[2];
    > > 15. return *ap->number???? which
    > > 16. resolves to t[1].num2 ;
    > > 17. }
    > >
    > > How do I set up the table of pointers in
    > > p[ ] so as to be able retrieve t[ix].num2,
    > > where ix can be 0, 1, or 2?

    >
    > Pointers p[3] = { { &t[0].num1 },
    > { &t[1].num2 },
    > { &t[2].num3 } };
    >
    > seems to answer you question, but since that is obvious, I must have
    > missed what you really want.


    He wants to use one variable (ix) to determine which element of t[] is
    accessed, and a different variable (ap) to determine which member of
    the struct to access. Your solution produces results which do not
    depend upon the value of ix in any way.

    Having seen your solution, I realize that there's a different
    interpretation of his request, for which neither of our answers is
    correct:

    Pointers p[3] = { {&t[ix].num1},
    {&t[ix].num2},
    {&t[ix].num3} };

    However, as with your answer, that feels a little too obvious to be
    the thing he's actually looking for.
    , Jan 22, 2008
    #4
  5. pete142

    pete142 Guest

    Re: pointer to a member of a struct which is in turn a member of anarray of structs

    On Jan 22, 1:02 pm, wrote:
    > pete142 wrote:
    > > Hi folks --

    >
    > > I have a 4-long array t[ ] of
    > > of struct Targets. And a table of
    > > int * in p[ ]. I need to set up
    > > the int * entries in p[ ] such
    > > that each can reference an int
    > > in any member of the Targets t[ ]
    > > array. How to do it?

    >
    > > Much-simplified example:

    >
    > >  1. #include <stdio.h>
    > >  2. int main(int argc, char* argv[ ]) {
    > >  3.   int ix;
    > >  4.   typedef struct { int num1;
    > >                        int num2;
    > >                        int num3;
    > >       } Targets;
    > >  5.   Targets t[4];
    > >  6.
    > >  7.   typedef struct { int *number; } Pointers;
    > >  8.   Pointers *ap;
    > >  9.   Pointers p[3] = { { &t[?].num1 },
    > > 10.                     { &t[?].num2 },
    > > 11.                     { &t[?].num3 } };
    > > 12.  ix = 1;           // point t[1]
    > > 13. // Need to reference t[ix] thru p[2]
    > > 14.   ap = &p[2];
    > > 15.   return *ap->number???? which
    > > 16.     resolves to t[1].num2 ;
    > > 17. }

    >
    > > How do I set up the table of pointers in
    > > p[ ] so as to be able retrieve t[ix].num2,
    > > where ix can be 0, 1, or 2?

    >
    > You cannot do what you want to do. There is no C syntax for that kind
    > of thing. C++ has pointers to members, but in C the closest you can
    > come to what you want to do is the following:
    >
    > #include <stddef.h>
    >
    > int main(int argc, char* argv[ ]) {
    >     int ix;
    >     typedef struct { int num1;
    >                        int num2;
    >                        int num3;
    >     } Targets;
    >     Targets t[4] = { {1,2,3}, {4,5,6}, {7,8,9}, {10,11,12}};
    >     typedef struct { size_t off; } Pointers;
    >     Pointers *ap;
    >
    >     Pointers p[3] = { { offsetof(Targets, num1) },
    >                      { offsetof(Targets, num2) },
    >                      { offsetof(Targets, num3) } };
    >     ix = 1;
    >     ap = &p[2];
    >
    >     return *(int*)((char*)(t+ix) + ap->off);
    >
    > }
    >
    > Please note: the C++ pointer to member does essentially the same thing
    > as the above code, it just simplifies the syntax considerably.- Hide quoted text -
    >
    > - Show quoted text -


    Thanks for your meticulous reply, James. offsetof is just what I was
    looking for despite its ugliness.

    -- Pete
    pete142, Jan 22, 2008
    #5
  6. Re: pointer to a member of a struct which is in turn a member of an array of structs

    writes:

    > Ben Bacarisse wrote:
    >> pete142 <> writes:
    >>
    >> > I have a 4-long array t[ ] of
    >> > of struct Targets. And a table of
    >> > int * in p[ ]. I need to set up
    >> > the int * entries in p[ ] such
    >> > that each can reference an int
    >> > in any member of the Targets t[ ]
    >> > array. How to do it?
    >> >
    >> > Much-simplified example:
    >> >
    >> > 1. #include <stdio.h>
    >> > 2. int main(int argc, char* argv[ ]) {
    >> > 3. int ix;
    >> > 4. typedef struct { int num1;
    >> > int num2;
    >> > int num3;
    >> > } Targets;
    >> > 5. Targets t[4];
    >> > 6.
    >> > 7. typedef struct { int *number; } Pointers;
    >> > 8. Pointers *ap;
    >> > 9. Pointers p[3] = { { &t[?].num1 },
    >> > 10. { &t[?].num2 },
    >> > 11. { &t[?].num3 } };
    >> > 12. ix = 1; // point t[1]
    >> > 13. // Need to reference t[ix] thru p[2]
    >> > 14. ap = &p[2];
    >> > 15. return *ap->number???? which
    >> > 16. resolves to t[1].num2 ;
    >> > 17. }
    >> >
    >> > How do I set up the table of pointers in
    >> > p[ ] so as to be able retrieve t[ix].num2,
    >> > where ix can be 0, 1, or 2?

    >>
    >> Pointers p[3] = { { &t[0].num1 },
    >> { &t[1].num2 },
    >> { &t[2].num3 } };
    >>
    >> seems to answer you question, but since that is obvious, I must have
    >> missed what you really want.

    >
    > He wants to use one variable (ix) to determine which element of t[] is
    > accessed, and a different variable (ap) to determine which member of
    > the struct to access. Your solution produces results which do not
    > depend upon the value of ix in any way.


    Ah well, then the practical method would be to make the num1-3 into an
    array rather than a struct but I think it may be better to wait for
    more info.

    --
    Ben.
    Ben Bacarisse, Jan 23, 2008
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. DanielEKFA
    Replies:
    8
    Views:
    580
    DanielEKFA
    May 16, 2005
  2. Bob Rock
    Replies:
    3
    Views:
    155
    Ming Chen [.NET MVP]
    Jun 17, 2004
  3. John Harvey
    Replies:
    0
    Views:
    148
    John Harvey
    Jan 9, 2006
  4. Jen
    Replies:
    3
    Views:
    213
    Robert Klemme
    Mar 23, 2011
  5. Tuan  Bui
    Replies:
    14
    Views:
    451
    it_says_BALLS_on_your forehead
    Jul 29, 2005
Loading...

Share This Page