0

I'm initializing a nested array by passing each row separately.

The result is unexpected. See:

(gdb) li
1   #include <stdbool.h>
2   #include <stdio.h>
3   
4   bool sa1[2] = (bool [2]){true,  false};
5   bool sa2[2] = (bool [2]){false, false};
6   bool sa3[2] = (bool [2]){false, false};
7   
8   bool sa[3][2] = {sa1, sa2, sa3};
9   
10  int main() {
(gdb) print sa1
$1 = {true, false}
(gdb) print sa2
$2 = {false, false}
(gdb) print sa3
$3 = {false, false}

So far, all is as expected. However:

(gdb) print sa
$4 = {{true, true}, {true, false}, {false, false}}

I expected the value of sa to contain sa1, sa2 and sa3, but it doesn't.

If I inline those expressions by hand, then it contains the expected values if I compile it with GCC, but not LLVM.

What is going on?

4

1 Answer 1

1
  bool sa[3][2] = {sa1, sa2, sa3};

It initiates this array with references to arrays sa1, sa1, sa2 converted to bool. And it compiles only because that those references are not NULL pointers thus they convert to true.

In standard C this should not even compile because static storage duration objects *have to be inilized by constant expressions. Variable is not a constant expression so it some king of GCC extensi on

Sign up to request clarification or add additional context in comments.

1 Comment

Yeah, I thought that could be happening. I was trying to check that by printing the pointers, but it wasn't obvious to see.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.