473,626 Members | 3,369 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to form a pointer to the n'th element of an array which is a class member

I have

struct X
{
double array[10];
};

I want to form a pointer to the 5th element of X::array. The type of
the pointer should be
"double X::*" or "double* X::*" or something along those lines.

Here is the code I tried:

struct X
{
double array[10];
double* ptr;
};

int main()
{
double (X::* junk1)[10] = &X::array; // ok

double* X::* junk2 = &X::array;
// error in all the compilers I've tried
// but if an array decays to a
// pointer to the first element of the array
// shouldn't a pointer to a an array member decay to a
// pointer to the first element of the array member?

double* X::* junk3 = &X::ptr; // ok

double* X::* junk4 = &X::array[0];
// error in all the compilers I've tried
// the X::array[0] refers to the first element of the array
// but the compiler thinks the use of X::array[0] is an error

return 0;
}

Jun 30 '06 #1
9 1805
re************* *@yahoo.com wrote:
I have

struct X
{
double array[10];
};

I want to form a pointer to the 5th element of X::array. The type of
the pointer should be
"double X::*" or "double* X::*" or something along those lines.

Here is the code I tried:

struct X
{
double array[10];
double* ptr;
};

int main()
{
double (X::* junk1)[10] = &X::array; // ok

double* X::* junk2 = &X::array;
// error in all the compilers I've tried
// but if an array decays to a
// pointer to the first element of the array
// shouldn't a pointer to a an array member decay to a
// pointer to the first element of the array member?

double* X::* junk3 = &X::ptr; // ok

double* X::* junk4 = &X::array[0];
// error in all the compilers I've tried
// the X::array[0] refers to the first element of the array
// but the compiler thinks the use of X::array[0] is an error

return 0;
}


Not sure how you'd use it. Generally, it is not possible. Just like
"an int member of A that is a member of B" in a declaration like this:

struct B {
struct A {
int a, b;
};
};

or similar. Only one level of complexity is allowed. Not "an element
of a member of", not "a member of a member of".

Just use the member_ptr/index pair.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 30 '06 #2
Hi (removeps-generic),

Plz sepcify your name.

re************* *@yahoo.com wrote:
struct X
{
double array[10];
};

I want to form a pointer to the 5th element of X::array. The type of
the pointer should be
"double X::*" or "double* X::*" or something along those lines. double (X::* junk1)[10] = &X::array; // ok


This works fine but I tried to assign values but I am getting
exceptions.

I have tried this way..

int main(int argc, char* argv[])
{
struct X xObj;

double X::*junk1 = (double X::*)&X::array;

double *temp = &(xObj.*junk 1);

for(int i = 0; i < 10; i++)
*(temp++) = (double)i;

temp = &(xObj.*junk 1);

for(i = 0; i < 10; i++)
cout << *(temp++) << endl;

return 0;
}

I think you can understand the code.

When I tried to access like as below I am getting errors.

int main(int argc, char* argv[])
{
struct X xObj;

double (X::*junk1)[10] = &X::array;

(xObj.*junk1)[0] = 1.0f; // crashes here

cout << (xObj.*junk1)[0];
return 0;
}

-- Murali Krishna

Jul 1 '06 #3
Murali Krishna wrote:
I have tried this way..

int main(int argc, char* argv[])
{
struct X xObj;

double X::*junk1 = (double X::*)&X::array;


Interesting. The type of X::array is a member of X which is an array
of 10 doubles. But (dobule X::*) means a member of X which is a
double. I'm confused why the assignment works, but hey, it's all good
if it does!

Thanks.

Jul 1 '06 #4
Hi,

re************* *@yahoo.com wrote:
Interesting. The type of X::array is a member of X which is an array
of 10 doubles. But (dobule X::*) means a member of X which is a
double. I'm confused why the assignment works, but hey, it's all good
if it does!
It works. I tested it.

I'll try to tell you what it does..

We generally do not write pointers this way. I mean.. I never did that.

I used to write like this..

double *pArray = &xObj.array; // Gives the address assigned in the
memory for array of xObj.

but when you write like this..
double (X::* junk1)[10] = &X::array;
or like this..
double X::*junk1 = (double X::*)&X::array;
You can see that the value is 0x000000. it took lot of time to
understand this.

next I added one more variable in the structure.
struct X
{
double array[10];
int i;
};
int X::*pInt = &X::i;

now when you try to get a pointer to i, you get 0x000050.

(8 Bytes for double * 10) = 80 base 10 = 50 base 16 (hex)

what I have understood is, it gives the offset. as double array[10] is
the first variable to start, the offset is 0 (the offset of &X::array).
so what happens when you access with X object is, it adds the offset
with the address of the object.

ex:

xObj.*pInt = 10; // *(&xObj + pInt (offset of int i)) // some thing
like this

it worked in the same way for..
double X::*junk1 = (double X::*)&X::array;
double *temp = &(xObj.*junk 1);
temp got the starting address of double array[10] allocated in the
memory for xObj.
(xObj.*junk1)[0] = 1.0f; // crashes here
that's because.. pointer to member variables or functions cannot use
arithmetic operations.
(I read that in Thinking in C++). I don't know if that is the actual
reason. That's just a guess.
I'm confused why the assignment works, but hey, it's all good
if it does!
double X::*junk1 = (double X::*)&X::array;
why leave it? we must understand.

double (X::*junk1)[10] is the pointer to array of 10 doubles

double X::*junk1 is the pointer to 1 double.

ok what happens in (double X::*)&X::array;

it takes the starting address of array, when assigning, the L value
must be capable of taking one double not 10. That's why we needed type
casting.

Tell me if you have tried some thing else.

Plz write your name. I dont want to talk to a computer.

-- Murali Krishna

Jul 2 '06 #5
re************* *@yahoo.com wrote:
Murali Krishna wrote:
>I have tried this way..

int main(int argc, char* argv[])
{
struct X xObj;

double X::*junk1 = (double X::*)&X::array;

Interesting. The type of X::array is a member of X which is an array
of 10 doubles. But (dobule X::*) means a member of X which is a
double. I'm confused why the assignment works, but hey, it's all good
if it does!
There is nothing "good" about it. A cast is used. The cast means
"hey, compiler, move over with your type checking, I am now in the
driving seat!". This particular version of the C-style cast is
equivalent to 'reinterpret_ca st', which only works if you intend
to store the value, and not use it. Besides, it only works if the
size of the storage (the left-hand side of the assignment) is large
enough to actually keep the right-hand side. There is no guarantee
of the latter, and the former is certainly not true (since the
intend is not to simply store it but to use it somehow). With both
requirements not satisfied, you have undefined behaviour in its
finest. Whatever you get is by pure luck or by the will of the
compiler implementors (whichever you consider better). Just don't
bring this code to *our* doorstep. Such code is *not* taken
seriously in any respectable programming shop.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 2 '06 #6
Victor Bazarov wrote:
Just don't
bring this code to *our* doorstep. Such code is *not* taken
seriously in any respectable programming shop.

V
Hi, it was just a try. I really dont know how to do that. I don't say
it is the correct implementation. Plz tell how to access pointer to
member variables of an array if you know.

Thanks..

-- Murali Krishna

Jul 3 '06 #7
Murali Krishna wrote:
Victor Bazarov wrote:
>Just don't
bring this code to *our* doorstep. Such code is *not* taken
seriously in any respectable programming shop.

V

Hi, it was just a try. I really dont know how to do that. I don't say
it is the correct implementation. Plz tell how to access pointer to
member variables of an array if you know.
You have two choices: use a pointer to double and form it from the
object that contains your array and then index it appropriately
(in this scenario no pointer to member is involved), or form a pointer
to member of the type 'array of double' and then for any object you
can access the member through the pointer to member, and then index it
as you normally would. As you've been already told, there is no C++
syntax to form "a pointer to an element of an array that is a member
of some class".

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 4 '06 #8
Victor Bazarov wrote:
re************* *@yahoo.com wrote:
Murali Krishna wrote:
double X::*junk1 = (double X::*)&X::array;
Interesting. The type of X::array is a member of X which is an array
of 10 doubles. But (dobule X::*) means a member of X which is a
double. I'm confused why the assignment works, but hey, it's all good
if it does!
There is nothing "good" about it. A cast is used. The cast means
"hey, compiler, move over with your type checking, I am now in the
driving seat!". This particular version of the C-style cast is
equivalent to 'reinterpret_ca st', which only works if you intend
to store the value, and not use it. Besides, it only works if the
size of the storage (the left-hand side of the assignment) is large
enough to actually keep the right-hand side. There is no guarantee
of the latter, and the former is certainly not true (since the
intend is not to simply store it but to use it somehow). With both
requirements not satisfied, you have undefined behaviour in its
finest. Whatever you get is by pure luck or by the will of the
compiler implementors (whichever you consider better). Just don't
bring this code to *our* doorstep. Such code is *not* taken
seriously in any respectable programming shop.
Good point. static_cast accepts the cast operation on my compiler.

double X::*junk1 = static_cast<dou ble X::*>(&X::array );

Does that make it any better?

Jul 4 '06 #9
Victor Bazarov wrote:
Not sure how you'd use it. Generally, it is not possible. Just like
"an int member of A that is a member of B" in a declaration like this:

struct B {
struct A {
int a, b;
};
};

or similar. Only one level of complexity is allowed. Not "an element
of a member of", not "a member of a member of".
Seems it should be possible.

struct X
{
double x0;
double x1;
double x2;
};

Here &X::x2 would be a pointer to the 3rd element of the array.

So why is it not possible with

struct X
{
double x[3];
};

I think one should be able to say &X::x[2].

Just use the member_ptr/index pair.
What do you mean?

Jul 4 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
4188
by: Michael Brennan-White | last post by:
If I submit my for using a get action the resulting page loads . If I use a post action I get an error page saying "The page cannot be found". I am calling the originating page!!! This happens in IE as well as FireFox. This code has been tested on a Win2003 server, IIS6, PHP 5.0.3, mySQL 4.1.8 and it works fine. The problem server is a Win2k server, IIS5, PHP 5.0.4, mySQL 4.1.11.
21
2071
by: MOvetsky | last post by:
Is the following code ISO C++ standard compliant? If yes, is it guaranteed that it will not crash on compliant platforms? If yes, will it print "Pointers are equal" on any compliant platform? Will answers be the same if p points to local memory or string literal? char *p = new char; char *p1 = p-1; p1 = p1 + 1;
3
5841
by: Kaz Kylheku | last post by:
Given some class C with array T x, is it possible to get a pointer-to-data-member to one of the elements? &C::x gives us a pointer-to-member-array: T (C::*). But I just want to get a T C::* pointing to a selected array element, so that I can later use an instance c of that class to pick out that array element: c->*ptr. This syntax, for instance, doesn't work: &C::x. My compiler thinks
10
4108
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; };
10
1625
by: houstorx | last post by:
I've been looking at the committee draft of the C99 specification, specifically the one at this URI: http://www.open-std.org/jtc1/sc22/wg14/www/docs/n843.pdf. I don't have a copy of the official international standard, but I assume that it is similar. The rules for pointer arithmetic (6.5.6.7-8) are appended below for ease of reference. They seem astonishingly restrictive. As far as I can see, there is no guarantee that the code:
23
3438
by: Kenneth Brody | last post by:
Given the following: char *ptr1, *ptr2; size_t n; ptr2 = ptr1 + n; Assuming ptr1 is a valid pointer, is the following guaranteed to be true? (ptr2 - ptr1) == n
2
2106
by: justplain.kzn | last post by:
Hi, I have a table with dynamic html that contains drop down select lists and readonly text boxes. Dynamic calculations are done on change of a value in one of the drop down select lists. Using Safari,my first iteration the script works fine ( indicating that there are 33 form variables ). When trying another dropdown select value, the
1
2818
by: Muchach | last post by:
Hello, Ok so what I've got going on is a form that is populated by pulling info from database then using php do{} to create elements in form. I have a text box in each table row for the user to enter input. I need to take this user input and put it back into the database. What would be the best method to do this. I can't use a normal post because the name of the text box is the same for each table row. I've heard that posting the...
0
3376
bmallett
by: bmallett | last post by:
First off, i would like to thank everyone for any and all help with this. That being said, I am having a problem retrieving/posting my dynamic form data. I have a form that has multiple options within options. I have everything being dynamically named from the previously dynamically named element. (I hope this makes sense.) I am not able to retrieve any of the dynamically created values. I can view them on the source page but can't pull them...
1
8364
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8504
tracyyun
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7193
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6125
isladogs
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5574
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4092
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4197
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2625
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 we have to send another system
1
1808
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.