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 : |
|
2 : |
|
3 : |
|
Answer: Option
Explanation:
Option B gives "Undefined structure in 'aa'" error.
Discussion:
29 comments Page 1 of 3.
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.
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).
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)
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.
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)
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.
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.
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.
Sandeep said:
1 decade ago
Option B is declaring a structure variable of the same type in the same structure where as option C is declaring a pointer to the structure variable of the same type. (used in linked lists. ).
B is not allowed as the structure variable declaration can't be done until the structure is declared.
B is not allowed as the structure variable declaration can't be done until the structure is declared.
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?
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.
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.
structure aa *var means pointer to structure itself... that is allowed.
Sundar said:
1 decade ago
Option 2, it can be used for creating linked list. A node should have have a link to next node of same type. For this purpose pointer to the same struct (type) is required.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers