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 2 of 3.

Abhisha said:   1 decade ago
We can create only a pointer of the structure an the same struct but struct <struct_name> <object>; is syntax for creating a reference to that struct. In this case struct x is not aa is not yet created. So 2nd is wrong.

Mosin said:   1 decade ago
Give diff b/w struct aa var and struct aa *var.

Rajesh said:   1 decade ago
struct aa var; - It is incorrect because we don't know the size of structure... before that only we declaring.

structure aa *var means pointer to structure itself... that is allowed.

Seema said:   1 decade ago
*var is the pointer variable of structure & var is variable of structre which cannot be created before creation of structre.

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.

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)

Kushal baldev said:   9 years ago
First declaration of the structure is done after that only you can declare the variable as the structure is mix collection of data types so if one doesn't know the size how can we declare a variable? Where as a pointer size is a fix of two bytes in the general scenario?

Bhanu prataap said:   9 years ago
I can't understand please give me the deep description why the answer is B?

Md kamal said:   8 years ago
I am not understanding this. Please, anyone explain this.


Post your comments here:

Your comments will be displayed after verification.