简体   繁体   中英

Question about C Syntax: Nested curly brackets for struct declaration mid-function?

Title may be a bit confusing, as I am having trouble describing it. I am sure this is already asked but I have no idea how to properly ask it and find it on the site.

Essentially, for C, I am looking through some kernel code and see that in some functions there is an additional 'nested' set of curly braces ('{}'), which always has a header comment of "TRACE" and contains info about a struct.

I am trying to figure out what this syntax is called and more about it in general. I would appreciate any and all help. Thanks so much! (Screen cap below)

Screencap of example code

This is just a compound statement. Anywhere you can have a statement, such as x = 3; or while (i) … , you can have a compound statement, { … } . Inside a compound statement, you can have declarations and statements.

Compound statements are frequently encountered as the statements used with if , while , or similar statements:

if (a < b)
{ // Start compound statement.
   foo(a);
   bar(b);
}

while (n--)
{ // Start compound statement.
    foo(p, n);
}

However, you may also have a compound statement on its own. This can be useful to declare identifiers inside the compound statement, which restricts their scope to that statement (called a block ). That reduces the opportunity for bugs (since an identifier is not visible outside its scope, it cannot be accidentally used outside that scope). Compound statements can also be used just for organization, to illustrate to the reader that the enclosed statements are a group that are related and conceptually separate to some extent from the statements before or after them.

The bodies of functions are also compound statements:

int foo(int a, int b)
{ // Start compound statement.
    return a+b;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM