GOEDUHUB Online Courses || Last Batch Student's Projects || COVID-19 Projects(AI-ML) || Universities  ||  Placement Preparation  Subscribe our youtube channel
+91-7976731765 Free Online Tutorials ||  MACHINE LEARNING || Python || DBMS || OOPs || DSA || Java || Linux/Unix ||  C Programming
Register For Online Free Certified Workshops 
0 like 0 dislike
11 views
in Tutorial & Interview questions by Goeduhub's Expert (8k points)

A distinctive syntactic peculiarity of C is that declarations mirror the use of the declared object as it would be in a normal expression.

1 Answer

0 like 0 dislike
by Goeduhub's Expert (8k points)
 
Best answer

A distinctive syntactic peculiarity of C is that declarations mirror the use of the declared object as it would be in a normal expression.

The following set of operators with identical precedence and associativity are reused in declarators, namely: 

  • the unary * "dereference" operator which denotes a pointer;
  • the binary [] "array subscription" operator which denotes an array; the (1+n)-ary 
  • () "function call" operator which denotes a function;
  • the () grouping parentheses which override the precedence and associativity of the rest of the listed operators.

The above three operators have the following precedence and associativity:

Operator               Relative Precedence Associativity

[] (array subscription) 1                                     Left-to-right

() (function call)

1

Left-to-right

* (dereference)

2

Right-to-left

 When interpreting declarations, one has to start from the identifier outwards and apply the adjacent operators in the correct order as per the above table. Each application of an operator can be substituted with the following English words:

Expression                                Interpretation

thing[X]             an array of size X of...

thing(t1, t2, t3) a function taking t1, t2, t3 and returning...

*thing               a pointer to...

 It follows that the beginning of the English interpretation will always start with the identifier and will end with the type that stands on the left-hand side of the declaration.
Examples

char *names[20];

[] takes precedence over *, so the interpretation is: names is an array of size 20 of a pointer to char.

char (*place)[10];

In case of using parentheses to override the precedence, the * is applied first: place is a pointer to an array of size 10 of char.

int fn(long, short);

There is no precedence to worry about here: fn is a function taking long, short and returning int.

int *fn(void);

The () is applied first: fn is a function taking void and returning a pointer to int.

int (*fp)(void);

Overriding the precedence of (): fp is a pointer to a function taking void and returning int.

int arr[5][8];

Multidimensional arrays are not an exception to the rule; the [] operators are applied in left-to-right order according to the associativity in the table: arr is an array of size 5 of an array of size 8 of int.

int **ptr;

The two dereference operators have equal precedence, so the associativity takes effect. The operators are applied in right-to-left order: ptr is a pointer to a pointer to an int.

Multiple Declarations

The comma can be used as a separator (*not* acting like the comma operator) in order to delimit multiple declarations within a single statement. The following statement contains five declarations: 

int fn(void), *ptr, (*fp)(int), arr[10][20], num;

The declared objects in the above example are: 

  • fn: a function taking void and returning int;
  • ptr: a pointer to an int;
  • fp: a pointer to a function taking int and returning int;
  • arr: an array of size 10 of an array of size 20 of int;
  • num: int.

Alternative Interpretation 

Because declarations mirror use, a declaration can also be interpreted in terms of the operators that could be applied over the object and the final resulting type of that expression. The type that stands on the left-hand side is the final result that is yielded after applying all operators. 

/*

Subscripting "arr" and dereferencing it yields a "char" result.

Particularly: *arr[5] is of type "char".

*/

char *arr[20]; 

/*

Calling "fn" yields an "int" result.

Particularly: fn('b') is of type "int".

*/

int fn(char); 

/*

Dereferencing "fp" and then calling it yields an "int" result.

Particularly: (*fp)() is of type "int".

*/

int (*fp)(void); 

/*

*  Subscripting "strings" twice and dereferencing it yields a "char" result.

Particularly: *strings[5][15] is of type "char"

*/

char *strings[10][20];

Related questions

 Placements:   List of companies | Logical Reasoning Questions | Quantitative Aptitude Questions | General English Questions | Technical-MCQ and Interview Questions
 Important Lists: List of NITsList of IITsList of Exams After Graduation | List of Engineering Entrance Examinations (UG/PG)College ReviewsCollege Fest, Events & WorkshopsKnowledge ShareTrainees/Interns After 15-04-2020
Exams & Cutoffs: JEE Main | JEE Advanced | GATE | IES | ISROList of PSUs || Cutoff-GATECutoff_IIT-JEECS-ScopeECE ScopeEE-Scope
 Download Previous Year Papers For:  GATE | IES | RAJASTHAN TECHNICAL UNIVERSITY (RTU-Kota)RPSC Technical Exams | ISRO
 Goeduhub
About Us | Contact Us   Social::   |  | 
...