If a struct is only used in one function, can I declare it in that function? Can I do this:
int func()
{
struct {
int a, b;
} s;
s.a=5;
return s.a;
}
gcc choked on it, but it emitted a very weird looking error that I couldn't understand instead of saying "Sorry, you can't do that".
Structure is a collection of different datatype variables, grouped together under a single name. General form of structure declaration. The structure declaration is as follows − struct tagname{ datatype member1; datatype member2; datatype member n; }; Here, struct is the keyword.
While the global structure defines the outer shape of the process dataset, the local structure provides its inner organization.
You can define structure globally and locally. If the structure is global then it must be placed above all the functions, so that any function can use it. On the other hand, if a structure is defined inside a function then only that function can use the structure.
Structures (also called structs) are a way to group several related variables into one place. Each variable in the structure is known as a member of the structure. Unlike an array, a structure can contain many different data types (int, float, char, etc.).
This is perfectly valid C89/C99/C11 code, this a structure with no tag and the object has block scope. Check C99 6.7.2.3p6 to see the identifier for the tag is optional.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With