C Programming - Declarations and Initializations - Discussion

Discussion Forum : Declarations and Initializations - Point Out Correct Statements (Q.No. 4)
4.
Which of the structure is incorrcet?
1 :
struct aa
{
    int a;
    float b;
};
2 :
struct aa
{
    int a;
    float b;
    struct aa var;
};
3 :
struct aa
{
    int a;
    float b;
    struct aa *var;
};
1
2
3
1, 2, 3
Answer: Option
Explanation:

Option B gives "Undefined structure in 'aa'" error.

Discussion:
29 comments Page 1 of 3.

Aman Saxena said:   6 years ago
According to me the, best possible reason can be:

Structure is a user-defined data type, so when we declare any variable using that data type, that var get the size of that datatype.

Now here in structure 2, we have declared the strct type variable, before the complete declaration of struct, hence it gives the error.

But in structure 3, we can define a pointer of the same type, as it is fixed that any pointer will always take a space of 4 bytes.
(5)

Jarvis said:   1 decade ago
2]

struct aa
{
int a;
float b;
struct aa var;
};

-----------------------------------
Here the code is trying to create an object of a struct which is BEING defined right now.OK? We can not make an object unless we COMPLETELY define the struct, got it?
-----------------------------------

3]

struct aa
{
int a;
float b;
struct aa *var;
};
---------------------------------
Whereas here, a SELF-REFERENTIAL POINTER is created which is allowed. For more details check out Linked-List DS program.
(4)

Sanjay sahu said:   10 years ago
Hi dude.

Actually we can not create structure variable until we don't know how many byte takes in the memory.

Now when we declare struct variable inside sturct declaration >> then we can determine the size of structure variable.

Now when we declare pointer sturct variable. Then compiler already know the size of pointer (Generally 2 byte in 16 bit compiler).

That's why we can declare any type pointer inside struct declaration same type also (Only problem in memory analysis how many byte takes).
(2)

Rashmi said:   5 years ago
Anyone please explain the right answer.
(1)

Suraj said:   7 years ago
B. is incorrect because there is no declaration of struct aa;

But in c there is pointer *aa so it can declare it self as a garbage value.
(1)

Prashant said:   7 years ago
You can not put a whole struct inside of itself because it would be infinitely recursive.

But you can put the address of another instance of a struct inside of a struct; which is what a pointer is. An address the size of SomeStruct* is always the same, so the compiler knows how much memory to make for one instance of the struct.
(1)

Prince Bansal said:   1 decade ago
We can conclude from this example:

void fun(int i)
{
fun(i);
}

The above function, without a base condition it behaves like an infinite loop. Similarly, when we create a structure variable inside structure's body it will create infinite variables (We can imagine in this way) that is not possible.

C.kumar said:   7 years ago
Thank you @Jarvis.

Sakthi said:   7 years ago
Shall I declare struct aa var; outside the structure? Can Anyone explain, please?

Bindu said:   8 years ago
Please explain briefly I can't understand.


Post your comments here:

Your comments will be displayed after verification.