A
struct
is a type that you construct to hold related values together:
struct
{
int n;
float f;
} dummy;
Or often better
typedef struct
{
int n;
float f;
} dummy;
So that when you create an instance of the struct, it contains both the integer and floating point values you need together (instead of creating individual variables for them each time you need them both). This is particularly useful when you want to keep collections of items "together" - instead of declaring two arrays:
int ns[100];
int fs[100];
and having to remember to update the right indexes for both you can declare an array of the
struct
:
dummy arrayOfReltedValues[100];
and teh sytem will "keep" related values together.
So when you want to access the values it's pretty simple, you just use dot syntax:
dummy dum;
...
dum.n = 666;
dum.f = 1.2345;
...
printf("%u:%f\n", dum.n, dum.f);
And you don't need to cast anything because the system "knows" what type each part of the
struct
is.