Reputation: 670
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
Upvotes: 5
Views: 27258
Reputation: 14112
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...
Upvotes: 2
Reputation: 1206
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]
Upvotes: 1
Reputation: 338
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.
Upvotes: 0
Reputation: 1494
The const
qualifier applies to variables or members.
To instantiate a const
variable, just specify const
during instantiation.
What const
does, is:
const
variable is created with a value which can be resolved during compilation, put the variable in the program memoryWhen 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.
Upvotes: 2
Reputation: 39208
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;
};
Upvotes: 7
Reputation: 206528
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.
Upvotes: 8
Reputation: 63190
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.
Upvotes: 2