"John Carson" <do***********@ datafast.net.au > wrote in message
news:40******@u senet.per.parad ox.net.au...
"Howard" <al*****@hotmai l.com> wrote in message
news:c5******** @dispatch.conce ntric.net "John Harrison" <jo************ *@hotmail.com> wrote in message
news:c5******** ****@ID-196037.news.uni-berlin.de... {
es->ptr_buffer[0] = 0;
es->ptr_numbers[2] = 7;
return;
}
Does the above function modify the {instance of
the} structure?
Can the pointer to the structure be passed as
pointing to const data?
Yes, but only because the struct contains pointers. If the struct
contained arrays the answer is no.
Why would it not be the case if the members were arrays instead of
pointers? The function takes a pointer (by value), so it points to
the same struct object (not a copy of it) as the one wherever it is
called from, right? Since it is the same struct and not a copy, the
members of that struct are the same members, regardless of whether
they're arrays, strings, pointers, or whatever. Am I missing
something here?
If the pointer to the struct points to something const, then the struct
must not be changed. If the struct contains pointers, then that means the
pointers cannot be changed. The data pointed to by the pointers is *not*
part of the struct, so it can be changed.
With arrays, by contrast, the array elements are part of the struct and
hence they cannot be changed.
That still does not make sense to me, and I've tested the idea and found
that's not the case at all.
I'm not sure what it is that you're saying might be const here...the struct
whose address is stored in the pointer? Or the pointer? The parameter to
the function does not specify a const pointer of a pointer to const struct,
so you'll get a compiler error if you try to pass either of those. I can
pass it a pointer to a const struct by casting away the constness when
assigning the address to the pointer, but that's not something you would
want to do. (And I don't see anything at all about const anything in the
original post.)
But in any case, I tried with both arrays and with pointers, and it most
certainly *does* modify the member, just as I expected. I also tried it
with a pointer to a const object, by casting away the constness when
assigning the address. Same result. I assign 'Z' and 999 respectively to
the members (both pointer and array) in my constructor, but then set those
to 'A' and 1 in my function, and I get 'A' and 1 as the results. Here's my
code:
#include <iostream>
struct Example
{
unsigned char* pC;
unsigned int* pI;
unsigned char C[1];
unsigned int I[1];
Example();
~Example();
};
Example::Exampl e()
{
pC = new unsigned char[1];
pI = new unsigned int[1];
pC[0] = 'Z';
pI[0] = 999;
C[0] = 'Z';
I[0] = 999;
}
Example::~Examp le()
{
delete pC;
delete pI;
}
void MyExampleFuncti on( Example* pE )
{
pE->pC[0] = 'A';
pE->pI[0] = 1;
pE->C[0] = 'A';
pE->I[0] = 1;
}
int main()
{
using namespace std;
Example E;
MyExampleFuncti on(&E);
cout << "E.pC[0] " << E.pC[0] << endl;
cout << "E.pI[0] " << E.pI[0] << endl;
cout << "E.C[0] " << E.C[0] << endl;
cout << "E.I[0] " << E.I[0] << endl;
const Example E2;
Example* pE = (Example*)&E2;
MyExampleFuncti on(pE);
cout << "pE->pC[0] " << pE->pC[0] << endl;
cout << "pE->pI[0] " << pE->pI[0] << endl;
cout << "pE->C[0] " << pE->C[0] << endl;
cout << "pE->I[0] " << pE->I[0] << endl;
return 0;;
}
-Howard