2021年4月10日星期六

What is this nameless variable? C

Take this code

int main() {       int *p = &(int){2};  }  

What is this? &(int){2} A variable without a name? What's the point of a nameless variable in C? How does the compiler know to keep it on the stack if it's not assigned to a name?

Furthermore, if I declare a nameless value such as

int main() {     (int){3};  }  

is there a way to get the address of this nameless variable back in a fully portable way? (fully portable as in, it's gonna work on every platform no matter how the stack is arranged)

After years of coding in C I had no idea you could declare nameless variables like that. Is there a reason you would want to use this over regular named variables?

https://stackoverflow.com/questions/67040831/what-is-this-nameless-variable-c April 11, 2021 at 10:04AM

没有评论:

发表评论