简体   繁体   中英

const struct declaration

Can someone tell me the difference between these two versions of a declaration of a structure?

struct S
{
  uint8_t a;
};

and

const struct S
{
  uint8_t a;
}

Followed by:

void main(void)
{
  struct S s = {1};
  s.a++;
}

Hint, i've tried both versions for S in Visual Studio C++ 2010 Express so I know that both compile with errors.

Is the "const struct" doing nothing at all? "const struct S s = {1};" certainly does, but that's not the issue at the moment.

Regards Rich

/********************************************/

I've just worked out what

const struct <typename> <{type}> <variable instances a, b, .., z>;

is doing:

When const is present before the "struct", all variable instances are const, as though they'd be defined with:

const struct <typename> a, b, z;

So it does do something, but not when there's no instance definitions in-line with the struct declaration.

Rich

A declaration of structure just defines the data type.

const qualifier appies to a variable not a data type. So adding const preceeding a struct declaration should be redundant at the most.

With:

const struct S
{
  uint8_t a;
};

The const qualifier there is nonsense, and may even cause a compilation error with some C compilers. gcc issues a warning.

The intent appears to be to declare the data type struct S . In this case, the proper syntax is:

struct S
{
  uint8_t a;
};
const struct S
{
  uint8_t a;
};

is not a valid construct.

This

const struct S
{
  uint8_t a;
} x;

could possibly be valid as you're declaring a variable x that is now const , meaning it cannot change.

The const qualifier applies to variables or members. To instantiate a const variable, just specify const during instantiation.

What const does, is:

  • during compilation, verify that only reads are performed on the const variables
  • if the const variable is created with a value which can be resolved during compilation, put the variable in the program memory

When const is applied to members, like in:

struct T {
    int const i;
    int j;
};

You can only (legally) assign the value i during the creation of the structure. You may be able to modify a const value (if the program memory sits in RAM and not ROM) by casting it to a non-const type ( const-cast ) but this is something you shouldn't do.

The typical usage of const-cast is when you use a library which does not specify the constness in function declarations, and your code does. At this point if you want to use it you have to trust it and cast parameters before calling its functions.

It is nonsense to use const keyword before struct .

If you are using gcc compiler, it shows you the following warning:

warning: useless type qualifier in empty declaration [enabled by default]

This is the only use I can think of:

const struct S {
        int a;
        int b;
} s;

This declares a struct and immediately creates an instance for it named s and at this point, a and b in s are initialized to 0 (please note that at this point s is a global variable in the translation unit which it has been declared in and can be externally linked to).

printf("a = %d\t b = %d\n", s.a, s.b); // a = 0     b = 0

If you try to set members of s , you will fail:

s.a = 1; //error: assignment of member ‘a’ in read-only object

So, s is not really useful here...unless you do something like:

const struct S {
        int a;
        int b;
} s =  { 1, 2 };

Now let's create another instance of the same struct (declaration is still same as above):

    struct S other;
    other.a = 1;
    other.b = 2;

    printf("a = %d\t b = %d\n", other.a, other.b); // a = 1     b = 2

The compiler will not complain anymore as other is not const! only s is const!

Now, what that const do, you may ask? let's try to change s:

s = other; // error: assignment of read-only variable ‘s’

That is all to it. If you did not need the compiler to allocate storage for s at the point of declaration and still needed an instance of s to be const you would just add const at the point of instantiating struct S (note the capital S!!)

Bonus 1

const struct S {
        int a;
        int b;
};

note that there is no small s anymore. At this point, GCC will warn you that const qualifier does not do anything!!!!

Bonus 2

If you want every instance of the struct to be const, that is its members can only be initialized at the point of definition you can do like (using typedef):

typedef const struct S {
        int a;
        int b;
} s;

// The wrong way
s thisIsAnS;
thisIsAnS.a = 1; //error: assignment of member ‘a’ in read-only object 

// The correct way
s thisIsAnS = { 1 , 2 }; //compiles fine, but you can not change a or b anymore

Conclusion

To me, this is just syntactic sugar and only adds unnecessary complexity to the code. But who am I to judge...

When you declare

const var;

then it allocate the some memory space for it but

struct var;

it was just an declaration compiler does not allocate any space for it. so it shows the error and in const struct you didn't declare any varible see the code so it shows error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM