Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

const structure & member pointers

0 views
Skip to first unread message

Thomas Matthews

unread,
Apr 15, 2004, 11:59:58 AM4/15/04
to
Hi,

Given a structure of pointers:
struct Example_Struct
{
unsigned char * ptr_buffer;
unsigned int * ptr_numbers;
};

And a function that will accept the structure:
void My_Function(struct Example_Struct * es)
{
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?

----------- Snip here before replying -------
Background
-----------
In my program I have a structure containing
pointers to messages, message data, and a
response buffer. The content of the message,
data and response buffer will be changed; but
their locations will not be. In other words,
I am changing the data the pointers point to
but not the value of the pointers.

PC-Lint says I can declare the pointer to
the structure as a pointer to const data.

------------
Crossposting
------------
This is crossposted to news:comp.lang.c,
news:comp.lang.c++ and news:alt.comp.learn.lang.c-c++
since it deals with a fundamental issue shared
by both the C and C++ language and may be of
interest to readers in all groups.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

John Harrison

unread,
Apr 15, 2004, 12:19:32 PM4/15/04
to

"Thomas Matthews" <Thomas_Matthews...@sbcglobal.net> wrote in
message news:2kyfc.636$ai7...@newssvr15.news.prodigy.com...

> Hi,
>
> Given a structure of pointers:
> struct Example_Struct
> {
> unsigned char * ptr_buffer;
> unsigned int * ptr_numbers;
> };
>
> And a function that will accept the structure:
> void My_Function(struct Example_Struct * es)
> {
> 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.

This part is C++ specific. This is a good example of why public data members
are almost always a bad idea. If the ptr_buffer and ptr_numbers members
where not public then you could control access to them and by including or
omitting const from the member functions that access ptr_buffer and
ptr_numbers you could control what consistuted a modification of the
Example_Struct struct. With public data you have no such control.

john


Howard

unread,
Apr 15, 2004, 1:40:14 PM4/15/04
to

"John Harrison" <john_an...@hotmail.com> wrote in message
news:c5mcmk$39mdd$1...@ID-196037.news.uni-berlin.de...

>
> "Thomas Matthews" <Thomas_Matthews...@sbcglobal.net> wrote in
> message news:2kyfc.636$ai7...@newssvr15.news.prodigy.com...
> > Hi,
> >
> > Given a structure of pointers:
> > struct Example_Struct
> > {
> > unsigned char * ptr_buffer;
> > unsigned int * ptr_numbers;
> > };
> >
> > And a function that will accept the structure:
> > void My_Function(struct Example_Struct * es)

There's no reason for the word "struct" in the parameter list above. (BTW,
is it legal to have it there?)

> > {
> > 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?

-Howard

Thomas Stegen

unread,
Apr 15, 2004, 1:45:48 PM4/15/04
to
Howard wrote:

>
>>"Thomas Matthews" <Thomas_Matthews...@sbcglobal.net> wrote in
>>message news:2kyfc.636$ai7...@newssvr15.news.prodigy.com...

>>>And a function that will accept the structure:


>>> void My_Function(struct Example_Struct * es)
>
>
> There's no reason for the word "struct" in the parameter list above. (BTW,
> is it legal to have it there?)

In C it is "illegal" to not have it there. So yes, there is plenty
of reason to have it there. Why this is crossposted so widely I
don't know though.

--
Thomas.

Thomas Stegen

unread,
Apr 15, 2004, 1:47:49 PM4/15/04
to
Thomas Matthews wrote:

> Hi,
>
> Given a structure of pointers:
> struct Example_Struct
> {
> unsigned char * ptr_buffer;
> unsigned int * ptr_numbers;
> };
>
> And a function that will accept the structure:
> void My_Function(struct Example_Struct * es)
> {
> es->ptr_buffer[0] = 0;
> es->ptr_numbers[2] = 7;
> return;
> }
>
> Does the above function modify the {instance of
> the} structure?

No, what the pointers point to is not part of the structure unless
they are pointing to a field in the structure. (That would be very
nice ;)

>
> Can the pointer to the structure be passed as
> pointing to const data?
>

Yep.

--
Thomas.


John Carson

unread,
Apr 15, 2004, 1:56:34 PM4/15/04
to
"Howard" <ali...@hotmail.com> wrote in message
news:c5mhdu$d...@dispatch.concentric.net

> "John Harrison" <john_an...@hotmail.com> wrote in message
> news:c5mcmk$39mdd$1...@ID-196037.news.uni-berlin.de...
>>
>> "Thomas Matthews" <Thomas_Matthews...@sbcglobal.net>
>> wrote in message news:2kyfc.636$ai7...@newssvr15.news.prodigy.com...
>>> Hi,
>>>
>>> Given a structure of pointers:
>>> struct Example_Struct
>>> {
>>> unsigned char * ptr_buffer;
>>> unsigned int * ptr_numbers;
>>> };
>>>
>>> And a function that will accept the structure:
>>> void My_Function(struct Example_Struct * es)
>
> There's no reason for the word "struct" in the parameter list above.
> (BTW, is it legal to have it there?)

Yes.

>>> {
>>> 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.

--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)


Howard

unread,
Apr 15, 2004, 2:31:37 PM4/15/04
to
>
> >>>And a function that will accept the structure:
> >>> void My_Function(struct Example_Struct * es)
> >
> >
> > There's no reason for the word "struct" in the parameter list above.
(BTW,
> > is it legal to have it there?)
>
> In C it is "illegal" to not have it there. So yes, there is plenty
> of reason to have it there. Why this is crossposted so widely I
> don't know though.
>
> --
> Thomas.
>

Oh, I didn't notice the corss-posting. I just hit Reply to Group, assuming
it was for the C++ language group, since that's the only one I read. Sorry.

-Howard


Howard

unread,
Apr 15, 2004, 2:56:35 PM4/15/04
to

"John Carson" <donald...@datafast.net.au> wrote in message
news:407e...@usenet.per.paradox.net.au...

> "Howard" <ali...@hotmail.com> wrote in message
> news:c5mhdu$d...@dispatch.concentric.net
> > "John Harrison" <john_an...@hotmail.com> wrote in message
> > news:c5mcmk$39mdd$1...@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::Example()
{
pC = new unsigned char[1];
pI = new unsigned int[1];

pC[0] = 'Z';
pI[0] = 999;
C[0] = 'Z';
I[0] = 999;
}

Example::~Example()
{
delete pC;
delete pI;
}

void MyExampleFunction( 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;
MyExampleFunction(&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;
MyExampleFunction(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

Howard

unread,
Apr 15, 2004, 2:58:04 PM4/15/04
to

"Howard" <ali...@hotmail.com> wrote in message
news:c5mlt3$d...@dispatch.concentric.net...

> 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

Should read "...does not specify a const pointer OR a pointer to a const
struct..."

-Howard


Howard

unread,
Apr 15, 2004, 3:46:16 PM4/15/04
to

"Howard" <ali...@hotmail.com> wrote in message
news:c5mlt3$d...@dispatch.concentric.net...
>

> (And I don't see anything at all about const anything in the
> original post.)

Sorry, didn't scroll down far enough for the const part of the discussion.
D'oh!

-Howard


John Harrison

unread,
Apr 15, 2004, 4:27:37 PM4/15/04
to

"Howard" <ali...@hotmail.com> wrote in message
news:c5mlt3$d...@dispatch.concentric.net...

You are missing the point.

This compiles

struct X
{
char* ptr;
};

int f(const X* x_ptr)
{
*x_ptr->ptr = 'a';
}

This does not

struct X
{
char array[1];
};

int f(const X* x_ptr)
{
*x_ptr->array = 'a';
}

That's the distinction I was making.

And the reasons are as John Carson said, the array is part of the X object,
whatever is pointed to by the ptr member of the X object is not part of the
X object.

john


Howard

unread,
Apr 15, 2004, 4:34:43 PM4/15/04
to
Ok, I see. I thought you were responding to the first question (does the
function modify the instance), not the second one (about passing a pointer
to const data). My mistake.

-Howard


John Harrison

unread,
Apr 15, 2004, 4:37:01 PM4/15/04
to

"Howard" <ali...@hotmail.com> wrote in message
news:c5mrl3$d...@dispatch.concentric.net...

> Ok, I see. I thought you were responding to the first question (does the
> function modify the instance), not the second one (about passing a pointer
> to const data). My mistake.
>

I took the first question to be a badly phrased version of the second
question, possibly my mistake.

john


Chris Torek

unread,
Apr 15, 2004, 4:37:11 PM4/15/04
to
Beware, heavy cross-posting. Beware furthermore: "const" has a
*different* meaning in C than in C++. In particular, C "const"
variables are actually variables, not constants:

const int seven = 7;
int i;
...
switch (i) {
case 3: ... code for i==3 ...
case seven: ... /* this line is an error in C, but OK in C++ */
}

In C "const" *always* means "read-only variable", much like
"extern const" in C++.

Summary of issue: Suppose "p" is a pointer to a const-qualified
structure or class variable, i.e., "const struct S *p" rather than
unqualified "struct S *p". What are the implications of an assignment
of the form "p->field[index] = new_value"? The answer depends on
whether the "field" member is a pointer or an array.

>"John Carson" <donald...@datafast.net.au> wrote in message
>news:407e...@usenet.per.paradox.net.au...

>> ... 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.

In article <news:c5mlt3$d...@dispatch.concentric.net>
Howard <ali...@hotmail.com> writes:
>That still does not make sense to me ...

Think of it as an "ownership issue". If I buy an array and lend it
to you, so that you point to it, *my* rules apply to that array,
even though you have borrowed it.

On the other hand, if *you* buy an array, *your* rules apply to it.

The const-ness of the array members depends on who "owns" them.
If the array is part of "struct S" (as a member named "field"), it
is the "struct S" that owns them; if the member named "field" is
just a pointer, it points to an array owned elsewhere, and the
const-ness of the array elements cannot be determined from the
const-ness of "*p".

>and I've tested the idea and found that's not the case at all.

I fear your test merely shows that the effect of undefined behavior
is often "hey, it works for me". :-) Alas, this is the sort of
thing that leads people to think that:

int i = 0;
...
f(i++, i++, i++); /* DANGER */

is well-defined and (depending on their system) always passes {2,
1, 0} or always passes {0, 1, 2} to f(). (Many systems actually
deliver one or the other fairly consistently.)

I snipped the original C++ example code, which was rather fatally
flawed for showing the problem (classes with constructors generally
need to be run-time modifiable even when const-qualified; you have
to stick to PODs *and* use static instances of each object).

Try this example instead (compile-able as both C *and* C++). First
the output:

% ln t.c++ t.c

[same program!]

% gcc -O -o t t.c -ansi -pedantic -Wall
% ./t
this was compiled with a C compiler
before:
P.field[0] = a
A.field[0] = a
modify P
P.field[0] = b
modify A
Segmentation fault (core dumped)
[C version crashes]

% gcc -O -o t t.c++ -Wall
% ./t
this was compiled with a C++ compiler
before:
P.field[0] = a
A.field[0] = a
modify P
P.field[0] = b
modify A
Segmentation fault (core dumped)
[C++ version crashes]

And the code:

#include <stdio.h>

struct ptr {
char *field;
};

struct arr {
char field[1];
};

void wr_ptr(const struct ptr *p) {
p->field[0] = 'b';
}

void wr_arr(struct arr *p) { /* cannot use "const" here */
p->field[0] = 'b';
}

static char buf[1] = { 'a' };
static const struct ptr P = { buf };
static const struct arr A = { { 'a' } };

struct trick2 { char x[1]; };

int main(void)
{
struct scopetrick { struct trick2 { char x[2]; } x; };

printf("this was compiled with a %s compiler\n",
sizeof ((struct trick2 *)0)->x == 2 ? "C" : "C++");

printf("before:\n");
printf(" P.field[0] = %c\n", P.field[0]);
printf(" A.field[0] = %c\n", A.field[0]);
fflush(stdout);

printf("modify P\n");
fflush(stdout);
wr_ptr(&P);
printf(" P.field[0] = %c\n", P.field[0]);
fflush(stdout);

printf("modify A\n");
fflush(stdout);
wr_arr((struct arr *)&A); /* cast required here */
printf(" A.field[0] = %c\n", A.field[0]);
fflush(stdout);

return 0;
}
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.

CBFalconer

unread,
Apr 16, 2004, 12:29:24 AM4/16/04
to
Howard wrote:
>> <Thomas_Matthews...@sbcglobal.net> wrote in

>>>
>>> Given a structure of pointers:
>>> struct Example_Struct
>>> {
>>> unsigned char * ptr_buffer;
>>> unsigned int * ptr_numbers;
>>> };
>>>
>>> And a function that will accept the structure:
>>> void My_Function(struct Example_Struct * es)
>
> There's no reason for the word "struct" in the parameter list
> above. (BTW, is it legal to have it there?)

Not only is it legal, but it is essential. Bear in mind this is
c.l.c.

It would be well to never reply to things cross-posted to c.l.c
and c.l.c++, apart from telling the OP to make up his mind.

--
fix (vb.): 1. to paper over, obscure, hide from public view; 2.
to work around, in a way that produces unintended consequences
that are worse than the original problem. Usage: "Windows ME
fixes many of the shortcomings of Windows 98 SE". - Hutchison


Howard

unread,
Apr 16, 2004, 11:18:57 AM4/16/04
to
The whole thing on my part was a misunderstanding of what was being
responded to. See my other post in this thread in response to John
Harrison. (Also, I was unaware of the cross-posting, and was thinking
purely in C++.) I understand perfectly the implications of using const, but
my example was not related to that, because of my misunderstanding of what
was being discussed. Sorry for the confusion.
-Howard


Howard

unread,
Apr 16, 2004, 11:20:11 AM4/16/04
to

"CBFalconer" <cbfal...@yahoo.com> wrote in message
news:407F51BA...@yahoo.com...

>
> It would be well to never reply to things cross-posted to c.l.c
> and c.l.c++, apart from telling the OP to make up his mind.
>

I had no intention of cross-posting...I simply hit the "Reply to Group"
button as always, not even noticing the cross-posting going on. Sorry.

-Howard

Ioannis Vranos

unread,
Apr 16, 2004, 12:20:37 PM4/16/04
to
"Thomas Matthews" <Thomas_Matthews...@sbcglobal.net> wrote in
message news:2kyfc.636$ai7...@newssvr15.news.prodigy.com...
> Hi,
>
> Given a structure of pointers:
> struct Example_Struct
> {
> unsigned char * ptr_buffer;
> unsigned int * ptr_numbers;
> };
>
> And a function that will accept the structure:
> void My_Function(struct Example_Struct * es)
> {
> es->ptr_buffer[0] = 0;
> es->ptr_numbers[2] = 7;
> return;
> }
>
> Does the above function modify the {instance of
> the} structure?

The members of Example_Struct are non-const pointers, so in the case of a
const Example_Struct, as with all included objects you *can not* change
their value (in this case the addresses they have stored) but you can change
the values of the pointed third objects.

>
> Can the pointer to the structure be passed as
> pointing to const data?


I suppose you mean const Example_Struct *es, yes.


Ioannis Vranos

Ioannis Vranos

unread,
Apr 16, 2004, 12:31:22 PM4/16/04
to
"Howard" <ali...@hotmail.com> wrote in message
news:c5mhdu$d...@dispatch.concentric.net...

>
> > > And a function that will accept the structure:
> > > void My_Function(struct Example_Struct * es)
>
> There's no reason for the word "struct" in the parameter list above.
(BTW,
> is it legal to have it there?)


It is legal and optional (but required in C).


> 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?


You can't change the contents of a const struct, but you can change the
value of third non-const objects pointed by non-const-for-modification
pointer variables inside that const struct.


int main()
{

struct whatever
{
char *p;
};

char array[5];

const whatever s={array};

// invalid sice s is const
// s.p=0;

// Valid since the object pointed is non-const
// and p is not const char *
s.p[2]='H';


}


Ioannis Vranos

Dave Thompson

unread,
Apr 19, 2004, 1:41:07 AM4/19/04
to
On 15 Apr 2004 13:40:14 EDT, "Howard" <ali...@hotmail.com> wrote:

>
> "John Harrison" <john_an...@hotmail.com> wrote in message
> news:c5mcmk$39mdd$1...@ID-196037.news.uni-berlin.de...

> > > struct Example_Struct <contents snipped>


> > > void My_Function(struct Example_Struct * es)
>
> There's no reason for the word "struct" in the parameter list above. (BTW,
> is it legal to have it there?)
>

As already noted it is certainly required in C -- unless you also
typedef struct X X, which can be combined with the struct declaration,
and is also legal though redundant in C++.

In C++ it is always legal, and is necessary if you have also declared
Example_Struct as an ordinary identifier (variable or enumerator) in
the same or closer scope (or a different typedef in closer scope),
which is a very bad idea but legal even though it is a very bad idea.
Did I mention it's a bad idea? :-)

- David.Thompson1 at worldnet.att.net

0 new messages