473,734 Members | 2,511 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

const structure & member pointers

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(str uct 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.l earn.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.l earn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Jul 22 '05 #1
19 6863

"Thomas Matthews" <Th************ *************** *@sbcglobal.net > wrote in
message news:2k******** *******@newssvr 15.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(str uct 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
Jul 22 '05 #2

"John Harrison" <jo************ *@hotmail.com> wrote in message
news:c5******** ****@ID-196037.news.uni-berlin.de...

"Thomas Matthews" <Th************ *************** *@sbcglobal.net > wrote in
message news:2k******** *******@newssvr 15.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(str uct 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

Jul 22 '05 #3
Howard wrote:
"Thomas Matthews" <Th************ *************** *@sbcglobal.net > wrote in
message news:2k******** *******@newssvr 15.news.prodigy .com...
And a function that will accept the structure:
void My_Function(str uct 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.

Jul 22 '05 #4
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(str uct 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.
Jul 22 '05 #5
"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...

"Thomas Matthews" <Th************ *************** *@sbcglobal.net >
wrote in message news:2k******** *******@newssvr 15.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(str uct 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)
Jul 22 '05 #6
>
And a function that will accept the structure:
void My_Function(str uct 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


Jul 22 '05 #7

"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



Jul 22 '05 #8

"Howard" <al*****@hotmai l.com> wrote in message
news:c5******** @dispatch.conce ntric.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
Jul 22 '05 #9

"Howard" <al*****@hotmai l.com> wrote in message
news:c5******** @dispatch.conce ntric.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
Jul 22 '05 #10

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

Similar topics

20
2503
by: Corno | last post by:
Hi all, There's probably a good reason why a const object can call non const functions of the objects where it's member pointers point to. I just don't see it. For me, that makes the the const keyword a lot less usable. Can anybody tell me what that good reason is? TIA,
2
3638
by: joe | last post by:
hi, after reading some articles and faq, i want to clarify myself what's correct(conform to standard) and what's not? or what should be correct but it isn't simply because compilers don't support. (first i compiled them with g++3.x. ERR means compiler will bark, otherwise it does accept it. Then the Comeau C/C++ 4.3.3 comes)
4
1978
by: Mahesh Tomar | last post by:
Dear Readers, I am porting my existing C code to C++. In my existing code there are numerous functions that has been defined with CONST qualifier. For eg. foo(const DATA_TYPE *x); DATA_TYPE is some typedef structure and offcourse x is a pointer to it. Needless to say my intention for writing such functions in C was to protect the accident write to x's content. So far so good. While porting to C++, I've mada DATA_TYPE as class and I want...
25
599
by: Thomas Matthews | last post by:
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:
2
2500
by: Pavel | last post by:
I am writing software for an embedded application and here is the question. GCC would emit data declared like const char text = "abc"; to .rodata (i.e. "read only data") section. I can put this section to flash memory and that would be OK. I have a structure with one member of it being const char** array;
11
2122
by: x-pander | last post by:
given the code: <file: c.c> typedef int quad_t; void w0(int *r, const quad_t *p) { *r = (*p); }
15
2133
by: Jiří Paleček | last post by:
Hello, I know the rules for const handling in C++, but I'd like to ask what is the "right" way to use them, eg. when is it appropriate to make a member function const? This came across this question when I was thinking about implementation of a class implementing some lazy data structure or cache. The C++ rules allow among other possibilities making all member functions non-const, or making all member functions const and all members...
0
1875
by: d3x0xr | last post by:
Heh, spelled out in black and white even :) Const is useles... do NOT follow the path of considering any data consatant, because in time, you will have references to it that C does not handle, and you'll be left with just noisy compiler warnings and confusion. if you start a project with all char *, and char ** and even char ***, if you begin at the low level weeding out references of 'passing const char * to char * ( such as...
4
2185
by: developereo | last post by:
Hi folks, Can anybody shed some light on this problem? class Interface { public: Interface() { ...} virtual ~Interface() { ...} virtual method() = 0; };
0
9449
Oralloy
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9310
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9236
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
9182
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...
1
6735
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
6031
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
4550
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...
1
3261
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
3
2180
bsmnconsultancy
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.