473,395 Members | 1,999 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 2408

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
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
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
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? ...
11
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
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 ==...
5
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...
17
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...
0
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*....
12
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:...
5
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...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...
0
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...
0
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...
0
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,...

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.