in Programming in C edited by
19,169 views
41 votes
41 votes

The following C declarations:

struct node { 
    int i:
    float j;
 };
 struct node *s[10];

define s to be:

  1. An array, each element of which is a pointer to a structure of type node
  2. A structure of $2$ fields, each field being a pointer to an array of $10$ elements
  3. A structure of $3$ fields: an integer, a float, and an array of $10$ elements
  4. An array, each element of which is a structure of type node
in Programming in C edited by
19.2k views

5 Answers

58 votes
58 votes
Best answer

Correct Option: A

$[]$ has greater precedence than $*$ in C. So, $s$ becomes an array of pointers.

edited by

1 comment

  1. C Right-Left Rule (Rick Ord's CSE 131 - UC San Diego) (ucsd.edu) - BRAHMASTRA rule to read complicated pointer statements
3
3
18 votes
18 votes

The reference id from *s[10] to the structure and not vice-versa, ruling out options B & C.
The main ambiguity lies among options A & D.
Declaration of the type : struct node s[10] == option D.
*s[10] is an array of 10 pointers, each of type struct node. Hence, option A is the correct answer.
The precedence among operators * & [] can also be another determining factor.
 

                                    

edited by
14 votes
14 votes

Option A

This is easily solvable by the spiral rule.

http://c-faq.com/decl/spiral.anderson.html


Even complex declarations can be figured out using this.

Source: https://stackoverflow.com/questions/34548762/c-isnt-that-hard-void-f

3 Comments

Nice one
0
0
Never came across with this! thanks :)
0
0
This needs to be bumped more.
0
0
3 votes
3 votes
Ans: A An array, each element of which is a pointer to a structure of type node
Answer:

Related questions