473,698 Members | 2,360 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

const struct *

I recently realized that I have a structure that I'd like to put more
protection on in the following sense: I'd like to modify

struct foo{
int *x;
} ;

to be:
struct foo {
const int *x;
}

I see only 2 choices (3 if I count not making the change):
1) modify the structure and all functions that initialize instances of
it
2) define struct const_foo {const int *x;}, change the formal
parameter list of all functions that used to take a const foo *
argument to take a const const_foo * instead, and cast all
calls.

I'm not particularly fond of either solution. #1 is aesthetically
appealing (ie, it's the proper thing to do), #2 would be much
faster to implement in this case, but seems to be a kludge.
It seems as if I'm implementing the const keyword to
make "const foo *" mean "const foo */w const *x" and
is pretty ugly.

Can anyone suggest another solution?

Nov 16 '05 #1
6 2439

are you sure that changing int *i to const int *i make any error? i
think it will not . it'll make errors only if you assing 'i' any value
twice.

Nov 16 '05 #2

Madguy wrote:
are you sure that changing int *i to const int *i make any error? i
think it will not . it'll make errors only if you assing 'i' any value
twice.


Things won't even compile if i simply add "const". Consider
the following code:

typedef struct { int *x;} foo_t;

void foo(foo_t *x){ *x->x = 3;}

int main()
{
foo_t x;
foo(&x);
return 0;
}

If you simply make to foo_t.x a const, then foo() is
writing to a read-only location. I think I'm actually asking a
slightly
more generic question:

suppose I have
struct foo_s {
int *a, *b, *c;
};

I want 3 functions, A(), B(), and C() which each take
a struct foo_s as an argument. A will only modify *a,
B will only modify *b, and C will only modify *c.
I'd like to reflect those facts in the parameter list
with const. The only solution I see for that is to
delcare 3 different structure types with const in
the appropriate place, and then either cast the
parameter for each call, or specify the parameter
as a void * and cast it within each function.

Nov 16 '05 #3
bill wrote:
If you simply make to foo_t.x a const, then foo() is
writing to a read-only location. I think I'm actually asking a
slightly
more generic question:

suppose I have
struct foo_s {
int *a, *b, *c;
};

I want 3 functions, A(), B(), and C() which each take
a struct foo_s as an argument. A will only modify *a,
B will only modify *b, and C will only modify *c.
I'd like to reflect those facts in the parameter list
with const. The only solution I see for that is to
delcare 3 different structure types with const in
the appropriate place, and then either cast the
parameter for each call, or specify the parameter
as a void * and cast it within each function.


You could do the following:

void A( const struct foo_s *p, int **toAlter )
{
... look hard a *p, update *toAlter
}

....
A( &someStructFooS , &someStructFooS .a );

Similarly for B & C.
--
Chris "another virtual machine" Dollin
time is just space under pressure.
Nov 16 '05 #4
bill wrote:
suppose I have
struct foo_s {
int *a, *b, *c;
};

I want 3 functions, A(), B(), and C() which each take
a struct foo_s as an argument. A will only modify *a,
B will only modify *b, and C will only modify *c.
I'd like to reflect those facts in the parameter list
with const. The only solution I see for that is to
delcare 3 different structure types with const in
the appropriate place, and then either cast the
parameter for each call, or specify the parameter
as a void * and cast it within each function.


That actually works very nicely, but it's not immediately
clear from the function declaration what's going on. The following
code won't compile, due to the illegal assignement (which is
desirable). However, the function declaration just has "void *"
and you have to look in the function to see what members
of the structure are constant. Any ideas on how to improve this?
(I'm not willing to simply declare the formal parameter of type
x_foo_t without casting all the calls, as I will not accept compile
time warnings. I am willing to do specific warning suppression,
but I don't think that's possible. (ie, tag the function with an
attribute indictating that this particular function shouldn't
generate that particular warning). Can that be done?)

typedef struct {
int *x;
int *y;
int *z;
} foo_t;

typedef struct { /* use this struct when only modifying *x */
int *x;
const int *y;
const int *z;
} x_foo_t;

void
mod_x(void *A)
{
x_foo_t *a = (x_foo_t*) A;
*a->x = 3;
*a->y = 3; /* compile time error!! */
}

int
main()
{
int a[1],b[1],c[1];
foo_t x;
x.x = a; x.y=b, x.z=c;
mod_x(&x);
printf("x=%d, y=%d, z=%d\n", *x.x, *x.y, *x.z);
return 0;

Nov 16 '05 #5
Chris Dollin wrote:
bill wrote:
If you simply make to foo_t.x a const, then foo() is
writing to a read-only location. I think I'm actually asking a
slightly
more generic question:

suppose I have
struct foo_s {
int *a, *b, *c;
};

I want 3 functions, A(), B(), and C() which each take
a struct foo_s as an argument. A will only modify *a,
B will only modify *b, and C will only modify *c.
I'd like to reflect those facts in the parameter list
with const. The only solution I see for that is to
delcare 3 different structure types with const in
the appropriate place, and then either cast the
parameter for each call, or specify the parameter
as a void * and cast it within each function.


You could do the following:

void A( const struct foo_s *p, int **toAlter )
{
... look hard a *p, update *toAlter
}

...
A( &someStructFooS , &someStructFooS .a );

Similarly for B & C.


I like that idea, but I can't seem to get around the compiler
warning that I'm discarding the qualifier. When I make the call:
A(&p, &p.x), attempting to alter p.x, I need to cast it as
A(&p, (int *)&p.x). That method makes it clearer from
the declaration, so that's a good thing. There's probably
no way to avoid the cast.... (eventually, the compiler warnings
will all get casted away....yeah, right)

Nov 16 '05 #6
bill wrote:
Chris Dollin wrote:
bill wrote:
> If you simply make to foo_t.x a const, then foo() is
> writing to a read-only location. I think I'm actually asking a
> slightly
> more generic question:
>
> suppose I have
> struct foo_s {
> int *a, *b, *c;
> };
>
> I want 3 functions, A(), B(), and C() which each take
> a struct foo_s as an argument. A will only modify *a,
> B will only modify *b, and C will only modify *c.
> I'd like to reflect those facts in the parameter list
> with const. The only solution I see for that is to
> delcare 3 different structure types with const in
> the appropriate place, and then either cast the
> parameter for each call, or specify the parameter
> as a void * and cast it within each function.


You could do the following:

void A( const struct foo_s *p, int **toAlter )
{
... look hard a *p, update *toAlter
}

...
A( &someStructFooS , &someStructFooS .a );

Similarly for B & C.


I like that idea, but I can't seem to get around the compiler
warning that I'm discarding the qualifier. When I make the call:
A(&p, &p.x), attempting to alter p.x, I need to cast it as
A(&p, (int *)&p.x). That method makes it clearer from
the declaration, so that's a good thing. There's probably
no way to avoid the cast.... (eventually, the compiler warnings
will all get casted away....yeah, right)


I suspect you've declared the struct as a const. You don't need to
to satisfy your request above starting "I want 3 functions, A(), B(),
and C()". Presumably there's more context than I know.

--
Chris "another virtual machine" Dollin
time is just space under pressure.
Nov 16 '05 #7

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

Similar topics

19
6850
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:
14
2458
by: Enrico `Trippo' Porreca | last post by:
Given: typedef struct Node Node; struct Node { void *obj; Node *next; }; typedef struct Stack Stack; struct Stack {
10
2062
by: S.Tobias | last post by:
1. If I have a struct type which contains a const member: struct mystruct { const int id; int mutable; }; does a definition struct mystruct ms; define an object `ms' which is *partly* const? Ie. the object `ms' is not const as a whole, but modifying ms.id yields UB in all contexts?
11
2119
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); }
18
1603
by: hzmonte | last post by:
typedef int t_compare_func(const void *, const void *); struct node *tree_search(struct node *root, const void *keyy, t_compare_func *comp) { struct node *cur_item; int result; if (root == NULL) return NULL; cur_item = root; while (cur_item != NULL) {
5
3324
by: Bill Pursell | last post by:
Suppose I have a structure with many members, and I pass a pointer to that structure to a function. I'd like the prototype of the function to specify that it will only be changing certain members of the function, but I have only the following horrific way to do that. Can someone please suggest a better alternative? #include <stdio.h> struct foo { char *a;
17
2436
by: Adrian Hawryluk | last post by:
Hi all, What is everyone's opinion of const inheriting? Should the object that a pointer is pointing at inherit the constness of the pointer? Such as in the case of a class having a pointer and then dereferencing that pointer. Should the dereferenced pointer have the same constness of the pointer as the pointer has the same constness as the class object? Yes, I am aware that C++ does not do this. I just want to know everyones...
0
1937
by: wellingj | last post by:
A little back ground on what I'm trying to do: I'm making a generic weighted graph class (vertexes and edges althought I don't call them that) to implement some pathfinding algorithms like A* and D*. I am also going to compare a grid map to a hex map, which is why I want to make a generic base graph class that gridmap and hexmap will inherit from. so here is the error I'm getting: TwoDMap.cpp: In member function 'void TwoDMap::setArc(const...
12
6245
by: hweekuan | last post by:
hi, it seems i can't assign the const variable u in class A, one way to solve the problem may be to build a copy constructor. however, why does C++ or vector class not like this code? my g++ is: gcc version 4.0.1 (Apple Inc. build 5465). thanks for the help. summary of compile error: --------------------------------------- cpp.C:4: error: non-static const member 'const unsigned int A::u',
5
1986
by: amvoiepd | last post by:
Hi, My question is about how to use const properly. I have two examples describing my problem. First, let's say I have a linked list and from it I want to find some special node. I write the function, and then figure that the function will not be modifying the list at all, so a const qualifier seems appropriate in the parameter. So essentially I have:
0
8678
marktang
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9166
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
8871
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
5861
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
4371
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
4621
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3052
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
2
2333
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2007
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.