Take a look at the code in C below that defines struct xyx
and initializes an object of that struct
:
#include <stdio.h> struct xyx { int x; int y; }; int main(void) { struct xyz a; a.x = 100; printf("%d\n", a.x); return 0; }
Executing the code above gives the error main.c:13:16
: storage size of ‘a’ isn’t known
.
Have a look at the code again!
In our
main
, we have created the object a of struct typexyz
; however, our struct declaration above had the namexyx
.
The compiler is unable to identify the amount of space that needs to be allocated to x
because there is no pre-defined or user-defined struct xyz
; hence, it gives the error that the storage size of a
is not known.
The solution is pretty simple, xyz
just needs to be changed to xyx
. Take a look at the working code below:
#include <stdio.h> struct xyx { int x; int y; char c; char str[20]; int arr[2]; }; int main(void) { struct xyx a; a.x = 100; printf("%d\n", a.x); return 0; }
RELATED TAGS
View all Courses