Member Avatar for I_m_rude

hi,
My question is about the const keyword.

union emp
{
    char name[];
    int age;
    float salary;
};

const union emp e1;

int main()
{
      strcpy(e1.name,"H");
      printf("%s",e1.name);
      e1.age=45;

      printf("%d",e1.age);
      printf("%f\n",e1.salary);

      return 0;
}

I know this code has error which is that "cannot modify constant objects". But Can anyone tell me how can i change the values of a constant union as We can't initalize it while defining union. So what's the way to correct this code snippet ?

For what purpose would you want a constant union? Anyway, you can initialize a union to the first listed member prior to C99, and you can use a designated initializer for an arbitrary member in C99 and later.

/* Earlier than C99 */
const union emp e1 = { "doodage" };

/* After C99 */
const union emp e1 = { .age = 45 };

But once again I'd question why you're declaring a union to be const when the entire point of unions is reuse of a single block of memory for multiple members when those members aren't needed simultaneously.

Member Avatar for I_m_rude

Actually sir, It's a question in my conceptual book. So it was there and when i done it, I have given wrong answer. So sir, Will you please tell how many and which one statements are wrong in the code I have given ? And sir, also tell me How are you so good (means exceptionally awesome) in these types of concepts ?

Will you please tell how many and which one statements are wrong in the code I have given ?

  • The code doesn't include <stdio.h>, which is technically OK except in the case of functions with variable length argument lists like printf(), where the behavior is undefined. Either way, best practice is to include the headers you need.

  • <string.h> isn't included either.

  • char name[]; is illegal because the array doesn't have a size. If it happens to compile, it's because of a compiler extension. In C99 the "struct hack" has been blessed as flexible array members. However, the member must be the last one in the structure, and the feature is questionable in writing robust code. Further, if I recall correctly that feature doesn't apply to unions.

  • const union emp e1; is exceptionally stupid, in my opinion. Of course there's the obvious problem of not initializing the union, but there's also the conceptual issue of a const union being nonsensical in the first place.

  • strcpy(e1.name,"H"); won't work because e1 is const, obviously.

  • e1.age=45; won't work because e1 is const, also obviously.

  • printf("%f\n",e1.salary); is unspecified behavior because you're requesting the value of a different member than the one previously stored (.age, in this case). Will it work? Probably, but it's not strictly required to do what you expect, and you'll need to consult your compiler's documentation to find out exactly what does happen.

    I can't confidently say that it's undefined behavior without consulting the C standard (though I'm reasonably sure), but interpreting the bytes of an integer as floating-point is risky at best. You'll probably get an unusual floating-point value as output, or 0 if printf() can't handle it.

The following code corrects all but the last bullet point, the removal of which alters the functionality of the program:

#include <stdio.h>
#include <string.h>

union emp
{
    char name[2];
    int age;
    float salary;
} e1;

int main(void)
{
    strcpy(e1.name, "H");
    printf("%s\n", e1.name);

    e1.age = 45;
    printf("%d\n", e1.age);

    printf("%f\n", e1.salary);

    return 0;
}

How are you so good (means exceptionally awesome) in these types of concepts ?

Experience and a perverse interest in the nitpicky details. ;) Case in point, I read language standards for fun.

Member Avatar for I_m_rude

the removal of which alters the functionality of the program:

So, in the float case statement, it will give unexpected o/p? Reason : I am using int (age) at that time. right ? for making this correct also, i should write

e1.salary=101.45f;

or something like that and then use printf statement for this. am i right ?

So, in the float case statement, it will give unexpected o/p? Reason : I am using int (age) at that time. right ? for making this correct also, i should write

e1.salary=101.45f;
or something like that and then use printf statement for this. am i right ?

Yes.

Member Avatar for I_m_rude

okies sir. i got it. thanks for solving my query. highly impressed! thanks a lot again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.