-1
error: unused variable 'part2' [-Werror,-Wunused-variable]

The error occurs only for part2 even though it's been initialized as well, just in a different manner. Is it just a compiler issue ?

int main(void)
{
    struct complex
    {
        int a;
        int b;
    };

    struct complex part1;
    part1.a = 2;
    part1.b = 3;

    struct complex part2 = {4, 5};

    struct complex part3 = {part3.a = 7, part3.b = 8};
}   
melpomene
  • 84,125
  • 8
  • 85
  • 148
callmeanythingyouwant
  • 1,789
  • 4
  • 15
  • 40
  • I wouldn't be surprised if your initialization of `part3` has undefined behavior. – melpomene Jun 09 '18 at 18:24
  • 2
    "Is it just a compiler issue ?" -- Given that unused variables are perfectly standard, and you specifically requested to have your compiler issue error messages for perfectly standard code, I'd say it's not so much a compiler issue as a user issue... –  Jun 09 '18 at 18:26
  • `part1` is initialized, then used (by assigning values to its fields). `part2` is initialized but not used. – Pete Becker Jun 09 '18 at 18:28
  • I get told `part2` is unused and `part1` is 'set but not used' (`[-Werror=unused-but-set-variable]`). The `part3` initializer is peculiar. It uses the result of the assignment of `7` to `part3.a` to initialize the `.a` member — it is **not** a designated initializer. It is close to undefined behaviour, modifiying `.a` twice. – Jonathan Leffler Jun 09 '18 at 18:41

1 Answers1

-1

As has been mentioned in the comments above, part1 is considered "used" because you assign values to its fields (a and b) explicitly in the following lines:

part1.a = 2;
part1.b = 3;

part2 is never used, only initialized in the line:

struct complex part2 = {4, 5};

part3 is more interesting, and I'm surprised your compiler is not throwing an error. By doing the following:

struct complex part3 = {part3.a = 7, part3.b = 8};

you are first assigning part3.a = 7 and part3.b = 8, then the results of those assignments are being used to initialize the structure (part3.a = 7 evaluates to 7, part3.b = 8 evaluates to 8). It essentially becomes the following set of statements:

part3.a = 7;
part3.b = 8;
struct complex part3 = {7, 8};

I would expect your compiler to throw an error because you are attempting to assign values to the fields of part3 before you have instantiated it. This is undefined behavior.

Joe L.
  • 82
  • 4
  • Thanks a lot! The part3 initialization I did is just like a designated initializer for arrays but for a struct. I got to know about it from this book that I'm learning from called, C: A Modern Approach. So I think it should be valid. – callmeanythingyouwant Jun 11 '18 at 07:49
  • 1
    @callmeanythingyouwant "The part3 initialization I did is just like a designated initializer for arrays but for a struct" -- No, it isn't. With designated initialisers, it would look like `struct complex part3 = {.a = 7, .b = 8};`. –  Jun 11 '18 at 13:13
  • Hey, yes. I had to check to be sure and you're right. It'll keep it in my head now, thanks. – callmeanythingyouwant Jun 11 '18 at 18:03