Understanding C/C++ style declarations!!!
There are certain rules known as Clockwise/Spiral rules to understand C/C++ style declarations.
It follows 3 simple rules:( see example below for better understanding).
- Starting with the unknown element, move in a spiral/clockwise direction; when encountering the following elements replace them with the corresponding English statements:
- [X] or []=> Array X size of… or Array undefined size of…
- (type1, type2)=> function passing type1 and type2 returning…
- *=> pointer(s) to...
2. Keep doing this in a spiral/clockwise direction until all tokens have been covered. 3. Always resolve anything in parenthesis first.
Example 1:
int* ptr;
We need to find, what is ptr?
Applying Spiral rule:
- ptr is a
- ptr is a pointer.. (because we encountered *)
Example 2:
char* str[10]
- str is an
- str is an array 10 of (we encountered [ )
Example 3:
const int* const ptr;
- ptr is a
- ptr is a constant
Limitations of spiral rule:
Example,
int (*foo[])();
Some cases where this rule cannot be applied.
- []() – cannot have an array of functions
- ()() – cannot have a function that returns a function
- ()[] – cannot have a function that returns an array.
Source: http://stringfeed.xyz/
"Dây cáp mạng Golden Link - CÁP MẠNG LÀ GÌ? LOẠI CÁP PHỔ BIẾN - CÁP MẠNG GOLDEN LINK MADE IN TAIWAN
ReplyDeleteDây cáp mạng Golden Link - CÁP MẠNG CAT5 LÀ GÌ? CAT5E LÀ GÌ? CAT6 LÀ GÌ?
Dây cáp mạng Golden Link Made in Taiwan "