Sunday, 30 August 2015

Easiest way to learn concepts in Computer Programming

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).


  1. 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 *)
cf












Example 2:
char* str[10]
  • str is an
  • str is an array 10 of (we encountered [ )

de
Example 3:
const int* const ptr;
  • ptr is a
  • ptr is a constant


xd
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/


1 comment: