Parenthesis Values ​​in C ++ and C

I just had a nasty bug in C ++. Therefore, I had a list of registers and values ​​that were wrapped in a structure, and then these structures are initialized in an array. But then I accidentally typed in ()instead {}. Here are some test codes:

#include <stdio.h>

struct reg_val {
        unsigned reg;
        unsigned val;
};

struct reg_val faulty_array[] = { 
        {0x5001, 0xff},
        {0x5580, 0x01},
        (0x5580, 0x02), //<- THIS LINE IS THE PROBLEM
        (0x5589, 0x00), //<- AND THIS LINE
};

struct reg_val good_array[] = { 
        {0x5001, 0xff}, 
        {0x5580, 0x01}, 
        {0x5580, 0x02},
        {0x5589, 0x00},
};

int main()
{
        unsigned i;
        unsigned faulty_size = sizeof(faulty_array) / sizeof(struct reg_val);
        printf("Size of faulty array: %d\n", faulty_size);

        for (i = 0; i < faulty_size; ++i) {
                printf("faulty reg: %x  val: %x\n", faulty_array[i].reg,
                       faulty_array[i].val);
        }   

        unsigned good_size = sizeof(good_array) / sizeof(struct reg_val);
        printf("\nSize of good array: %d\n", good_size);
        for (i = 0; i < good_size; ++i) {
                printf("good reg: %x  val: %x\n", good_array[i].reg,
                       good_array[i].val);
        }   
        return 0;
}

I am more familiar with C and, to my surprise, this is still compiled with g ++:

$ g++ -Wall array.cc
array.cc:11: warning: left-hand operand of comma has no effect
array.cc:12: warning: left-hand operand of comma has no effect
array.cc:13: warning: missing braces around initializer for ‘reg_val’
$ ./a.out 
Size of faulty array: 3
faulty reg: 5001  val: ff
faulty reg: 5580  val: 1
faulty reg: 2  val: 0       <-- the first value gets discarded as mentioned in the compiler warning

Size of good array: 4
good reg: 5001  val: ff
good reg: 5580  val: 1
good reg: 5580  val: 2
good reg: 5589  val: 0

This code obviously will not be able to compile with the C compiler, what's the difference in C ++ that makes the C ++ compiler (albeit reluctantly) accept this code?

+5
source share
3 answers

To answer your question, I will first answer: why can't this be compiled in C? Well, it does not compile due to:

initializer element is not constant

For a good measure, leave {}from C:

struct reg_val faulty_array[] = { 
        {0x5001, 0xff},
        {0x5580, 0x01},
        0x5580, 0x02, //<- THIS LINE IS THE PROBLEM
        0x5589, 0x00, //<- AND THIS LINE
};

:

Size of faulty array: 4
faulty reg: 5001  val: ff
faulty reg: 5580  val: 1
faulty reg: 5580  val: 2
faulty reg: 5589  val: 0

C ( ++). C ( ++) ( ). C, , . C (0x5580, 0x02) .

() ++, ++ , :

struct reg_val faulty_array[] = { 
        {0x5001, 0xff},
        {0x5580, 0x01},
        0x02,
        0x00,
};

... , , .

struct reg_val faulty_array[] = { 
        {0x5001, 0xff},
        {0x5580, 0x01},
        {0x02, 0x00},
};
+4
+4

++ , , . .

C , =) (, @BenVoigt)

+3

All Articles