Online Courses
Free Tutorials  Go to Your University  Placement Preparation 
Goeduhub's Online Courses @ Udemy in Just INR 570/-
Online Training - Youtube Live Class Link
0 like 0 dislike
117 views
in Tutorial & Interview questions by Goeduhub's Expert (8.3k 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.

Goeduhub's Top Online Courses @Udemy

For Indian Students- INR 360/- || For International Students- $9.99/-

S.No.

Course Name

 Coupon

1.

Tensorflow 2 & Keras:Deep Learning & Artificial Intelligence

Apply Coupon

2.

Natural Language Processing-NLP with Deep Learning in Python Apply Coupon

3.

Computer Vision OpenCV Python | YOLO| Deep Learning in Colab Apply Coupon
    More Courses

1 Answer

0 like 0 dislike
by Goeduhub's Expert (8.3k 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];

3.3k questions

7.1k answers

394 comments

4.6k users

Related questions

0 like 0 dislike
1 answer 110 views
0 like 0 dislike
1 answer 128 views
0 like 0 dislike
1 answer 221 views
0 like 0 dislike
1 answer 117 views

 Goeduhub:

About Us | Contact Us || Terms & Conditions | Privacy Policy || Youtube Channel || Telegram Channel © goeduhub.com Social::   |  | 
...