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 5 1435
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.
pete142 <pe*****@gmail.comwrites:
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 wrote:
pete142 <pe*****@gmail.comwrites:
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.
On Jan 22, 1:02*pm, jameskuy...@verizon.net 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 ja*********@verizon.net writes:
Ben Bacarisse wrote:
>pete142 <pe*****@gmail.comwrites:
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Kieran Simkin |
last post by:
Hi,
I wonder if anyone can help me, I've been headscratching for a few hours
over this.
Basically, I've defined a struct called cache_object:
struct cache_object {
char hostname;
char ipaddr;...
|
by: mrhicks |
last post by:
Hello all,
I need some advice/help on a particular problem I am having. I have
a basic struct called "indv_rpt_rply" that holds information for a
particular device in our system which I will...
|
by: Alexei A. Frounze |
last post by:
Hi all,
I have a question regarding the gcc behavior (gcc version 3.3.4).
On the following test program it emits a warning:
#include <stdio.h>
int aInt2 = {0,1,2,4,9,16};
int aInt3 =...
|
by: Zero |
last post by:
Hi everybody!
I have the following code:
struct Infos
{
char Haarfarbe;
int Groesse;
};
struct Person
|
by: mailtogops |
last post by:
Hi,
I have this question in mind for many years..
C++ class provides encapsulation which encapsulate its members and
member function.
class Experiment
{
private:
| |
by: aegis |
last post by:
The following was mentioned by Eric Sosman
from
http://groups.google.com/group/comp.lang.c/msg/b696b28f59b9dac4?dmode=source
"The alignment requirement for any type T must be a
divisor of...
|
by: Neil |
last post by:
What I am doing wrong
This works
batPointer = adaptors.adaptor->batData;
adaptors.batteries = batPointer->battery;
where:
batData is a pointer to a struct
batPointer is a pointer to a...
|
by: Szabolcs Borsanyi |
last post by:
Deal all,
The type
typedef double ***tmp_tensor3;
is meant to represent a three-dimensional array. For some reasons the
standard array-of-array-of-array will not work in my case.
Can I...
|
by: Stephan Beal |
last post by:
Hi, all!
Before i ask my question, i want to clarify that my question is not
about the code i will show, but about what the C Standard says should
happen.
A week or so ago it occurred to me...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |