0
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>

int main(void) {
    char ch[] = "test";
    bool X[32];
    char * pos = (char *)X;
    for (int i = 0; i < 4; ++i) {
        pos[i] = ch[i];
    }
    printf("%d\n", X[0]);
    return 0;
}

output: 116

There are both int type number that is not 1 or 0 and bool type 'false' in the array.

As the following picture.

VS code debug: enter image description here

Weideng
  • 1
  • 1
  • Please copy&paste text to your question as text instead of showing screenshots whenever possible. – Bodo Sep 25 '20 at 11:35
  • 2
    You intentionally cast the `bool` array to a `char*` and assign `char` values. Of course this prevents a normal conversion from `char` to `bool` which would convert any non-zero value to 1 (= `true`). If you assign to `X[i]` instead of `pos[i]` you will see the value `1` as expected. – Bodo Sep 25 '20 at 11:38
  • 2
    'Why does a hole appear in my foot when I shoot it?', is basically what you are asking. If you force the compiler to form invalid booleans, you get undefined behaviour, so just don't do that. – underscore_d Sep 25 '20 at 11:48

0 Answers0