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

P

pete142

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
 
B

Ben Bacarisse

pete142 said:
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.
 
J

jameskuyper

pete142 said:
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.
 
J

jameskuyper

Ben said:
pete142 said:
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.
 
P

pete142

pete142 said:
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
 
B

Ben Bacarisse

Ben said:
pete142 said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top