Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #163840
| From | Tim Rentsch <tr.17687@z991.linuxsc.com> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: About struct fields that are assigned once and never changed again |
| Date | 2021-12-15 00:25 -0800 |
| Organization | A noiseless patient Spider |
| Message-ID | <864k7a5l6q.fsf@linuxsc.com> (permalink) |
| References | <56b509db-c5ba-4020-b1e4-6250d41a706cn@googlegroups.com> |
Oğuz <oguzismailuysal@gmail.com> writes:
> I have an array of structs as follows:
>
> struct {
> struct {
> int value;
> bool in_use;
> } values[MAX_INPUTS_SIZE];
> size_t size;
> } inputs;
>
> and a function that initializes it with user-supplied data. After
> the function is run, nothing in the program alters `inputs.size'
> or `inputs.values[N].value'. Now I have two questions about this
> that I can't answer myself:
>
> i) Is there a way to treat size and value fields as `const'
> outside that function?
Here is one well-known technique. Step one: write a .h file
that defines the struct type and declares an extern pointer
variable and a function to do the initializing. Note that the
type of the pointer variable says it points to a 'const' struct:
/* file provide-struct.h */
#ifndef H_PROVIDE_STRUCT_H
#define H_PROVIDE_STRUCT_H
#include <stddef.h>
enum { MAX_INPUTS_SIZE = 10000 };
typedef struct {
struct {
int value;
_Bool in_use;
} values[ MAX_INPUTS_SIZE ];
size_t size;
} Values_Array_Struct;
extern const Values_Array_Struct *const inputs;
extern void initialize_inputs_struct( const char * );
#endif/*H_PROVIDE_STRUCT_H*/
Step two: write a .c file that defines a static instance of the
struct, along with a definition for the pointer variable and the
initializing function. Because the static struct instance is
declared (and defined) without using 'const', the initialization
function can write its values with no problem:
/* file provide-struct.c */
#include "provide-struct.h"
static Values_Array_Struct inputs_w;
const Values_Array_Struct *const inputs = &inputs_w;
void
initialize_inputs_struct( const char *data ){
inputs_w.size = 3;
inputs_w.values[0].value = 3;
inputs_w.values[2].value = 4;
inputs_w.values[0].in_use = 1;
inputs_w.values[2].in_use = 1;
/* ... etc ... */
}
Step three: write client code in a different .c file, which can
initialize the struct by calling the initialization function, and
access the struct using the declared pointer variable. Because
the pointer variable's type says it points to a 'const' struct,
the struct cannot be modified by client code:
/* file provide-struct-client.c */
#include "provide-struct.h"
#define inputs (*inputs)
unsigned
example_client( const char *data ){
unsigned i;
unsigned total = 0;
initialize_inputs_struct( data );
/* Any attempt to modify a field of 'inputs' will
be flagged by the compiler. */
for( i = 0; i < inputs.size; i++ ){
if( inputs.values[i].in_use ){
total += inputs.values[i].value;
}
}
return total;
}
Incidentally, notice the #define in this file. This macro
definition uses an idiom so the struct can be accessed using '.'
rather than '->'. Doing that lets the code look a little nicer
but isn't an essential element of the basic idea.
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
About struct fields that are assigned once and never changed again Oğuz <oguzismailuysal@gmail.com> - 2021-12-13 02:49 -0800
Re: About struct fields that are assigned once and never changed again Oğuz <oguzismailuysal@gmail.com> - 2021-12-13 02:50 -0800
Re: About struct fields that are assigned once and never changed again Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2021-12-13 03:05 -0800
Re: About struct fields that are assigned once and never changed again David Brown <david.brown@hesbynett.no> - 2021-12-13 12:12 +0100
Re: About struct fields that are assigned once and never changed again Oğuz <oguzismailuysal@gmail.com> - 2021-12-13 09:01 -0800
Re: About struct fields that are assigned once and never changed again David Brown <david.brown@hesbynett.no> - 2021-12-13 18:37 +0100
Re: About struct fields that are assigned once and never changed again scott@slp53.sl.home (Scott Lurndal) - 2021-12-13 18:06 +0000
Re: About struct fields that are assigned once and never changed again Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-12-13 16:12 +0000
Re: About struct fields that are assigned once and never changed again James Kuyper <jameskuyper@alumni.caltech.edu> - 2021-12-13 12:09 -0500
Re: About struct fields that are assigned once and never changed again Guillaume <message@bottle.org> - 2021-12-13 19:15 +0100
Re: About struct fields that are assigned once and never changed again James Kuyper <jameskuyper@alumni.caltech.edu> - 2021-12-13 13:25 -0500
Re: About struct fields that are assigned once and never changed again Tim Rentsch <tr.17687@z991.linuxsc.com> - 2021-12-15 00:25 -0800
Re: About struct fields that are assigned once and never changed again Oğuz <oguzismailuysal@gmail.com> - 2021-12-15 12:38 +0300
csiph-web