'What is the meaning of casting an integer number wrapped by curly braces in C language?
Happened to see a usage like this:
uint8_t a = 0x1a;
...
foo(&(uint32_t){ a })
...
So I tried below code snippet:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, const char * argv[])
{
uint8_t a = 0x1a;
printf("0x%x\n", a);
printf("%p\n", &a);
printf("%p\n", &a + 1);
printf("===============\n");
printf("0x%x\n", (uint32_t){a});
printf("%p\n", &(uint32_t){a});
printf("%p\n", (&(uint32_t){a} + 1));
printf("===============\n");
printf("%d\n", (&(uint32_t){a} + 1) == (&(uint32_t){a}));
printf("%ld\n", (&(uint32_t){a} + 1) - (&(uint32_t){a}));
return 0;
}
On M1 MacBook, compiled successfully and ran:
$ gcc try.c -o try.bin
$
$ ./try.bin
0x1a
0x16bb773ef
0x16bb773f0
===============
0x1a
0x16bb773e4
0x16bb773e4
===============
0
2
I got confused with below questions based on the printing:
- Is
(uint32_t){a}just a casting fora(like(uint32_t)a) in C language ?- If no, the values are the same
- If yes, the addresses are different
- Is
&(uint32_t){a}and&(uint32_t){a} + 1same in this case ?- If no, the addresses are printed same
- If yes,
==and-proved they are different
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
