#include<stdio.h>
#include<string.h>
union employee
{
char name[15];
int age;
float salary;
};
As already noted it doesn't make sense for these to be a union, but
I'm guessing this was a teaching example and someone liked the joke.
Also, it doesn't matter at your early stage, but in general it's a bad
idea to use floating-point (and especially float=single) for decimal
money amounts; if someday you reach the point of actually writing
financial/business software in C, remember that. Of course,
it's plausible that this 'application' is for a hypothetical firm that
makes all salaries be integer dollars -- or, heck, integer yen.
In the absence of collective bargaining or government regulation,
I can easily imagine that being popular with people.
const union employee e1;
int main()
{
e1.age=10; //error in gcc
e1.name="A"; //error in gcc
strcpy(e1.name,"A"); //warning in gcc
here e1 is const,then why line strcpy(e1.name,"A"); is just showing
warning in gcc,why not error as i am trying to change const
parameter.
Whereas line e1.name="A"; is an error.
Both of these -- assigning directly to a field like e1.age, or calling
a routine that takes a pointer-to-nonconst like strcpy(e1.name,) --
are constraint violations. The standard requires a diagnostic; either
an error or warning is fine as far as the standard is concerned.
Although I can't speak for the gcc implementors, a logical reason for
them to have made these different is that e1.fld=x is always wrong,
but func_of_nonconst(&e1.fld) is only wrong if the function actually
stores through that pointer. Before C89 there was no 'const', so all
code written before then, plus much code written after then by people
who learned before, or didn't care about const, or even disliked* it,
doesn't say const but is often actually okay, and historically it's
been the 'bias' of the C community for compilers to err toward the
side of trusting the programmer and accepting possibly-bad code.
OTOH in this case strcpy is (and must be) a standard-library function,
which the compiler can know about -- and gcc does know about some
'builtins' including strcpy for other purposes -- so it could have
realized this case is definitely wrong. I guess they figured that the
warning is already there and the extra effort to make it an error in
this case isn't worth it.
Note that for gcc you can make (all) warnings fatal just by using the
flag -Werror, if you want. Some other compilers have functionally
similar options; this is not an uncommon desire. Some shops use it on
checkin as a simple way to enforce a 'no warnings' rule.
* In particular, if after writing (or just specifying) (some of) your
code you decide to make a reference in a lower level routine const
that wasn't before, you usually must change some higher level ones to
follow suit -- sometimes a large number of them. This is disparaged as
'const poisoning' by people who consider it not worth the trouble, and
I for one don't say they're always wrong.
Note that e1.name = "A" violates another constraint as well; you can't
assign to any array, even one that isn't const.
fun(&e1);
printf("%s %d %f\n",e1.name,e1.age,e1.salary);
return 0;
}
int fun(const union employee *e)
{
strcpy(e->name,"B");
return 0;
}
This function wasn't prototype-declared at the point you called it,
which is why the compiler isn't required to (and didn't) diagnose this
equally-wrong access. If you move this function before main(), or put
a prototype declaration of it before main() (or even inside main()
which is legal but unusual), you'll get a diagnostic.