C reading complex pointer expression

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

Before We Learn How to Read Complex Array we should first know precedence and associative .

  • Precedence : Operator precedence describes the order in which C reads expressions
  • Associativity : Order operators of equal precedence in an expression are applied

Before Reading Article You Should know Precedence and Associative Table

Operator Precedence Associative
(),[] 1 Left to Right
*,Identifier 2 Right to Left
Data Type 3

Different Terms From Table –

() Bracket operator OR function operator.
[] Array subscription operator
* Pointer operator
Identifier Name of Pointer Variable
Data type Type of Pointer

How to Read ?


 
char (* ptr)[5]

Step 1 :

  • Observe Above Precedence Table
  • [] and () have Same Priority
  • Using Associativity , Highest Priority Element is decided
  • Associativity is from Left to Right First Priority is Given to “()”
 

 

Step 2 :

  • Between Pair of Brackets again we have to decide which one has highest priority ? ‘*’ or ‘ptr’ ?
  • * and Identifier ptr have Same Priority
  • Associativity is from Right to Left First Priority is Given to “ptr”
 
  
 

Step 3 :

  • Assign third Priority to [].
  • Write 3 under []
 
  
 

Step 4 :

  • Here Char is Data Type
  • Assign Priority 4 to char

 
 

Read it as –

ptr is pointer to a one dimensional array having size