Shivendra Mishra
Shivendra Mishra

Reputation: 688

Different syntax of structure in C

What are the different syntax of c structure? How do decode this :

struct p {
    char a[1];
    int b;
    int *a;
    int value;
};
struct p q[] = {
    {"a",      0, &b, C},
    {"J",      0, &k, l}
};

I found a another discussion here but didn't encounter this type.

Upvotes: 1

Views: 146

Answers (2)

tom
tom

Reputation: 354

That is declaring an array called q of type struct p, it is not actually defining a struct at all. The {"a", 0, etc...}'s inside the initialization list are creating the structs that populate the first and second element of the array by defining values for the structs fields, and in doing so, creating instances of the struct on the stack.

struct p appears to contain a char*,

an integer,

some other pointer

and something else(probably an integer).

Upvotes: 1

Vagish
Vagish

Reputation: 2547

q is the array of structure of type p.

p has following elements:

  1. char pointer

  2. any type (int,char,short etc. even a pointer is possible)

  3. pointer to any type

  4. should be a constant

The syntax is actually initializing q[0] and q[1]

Upvotes: 5

Related Questions