When programming, it is often convenient to have a single name with which to refer to a group of a related values. Structures provide a way of storing many different values in variables of potentially different types under the same name. This makes it a more modular program, which is easier to modify because its design makes things more compact. Structs are generally useful whenever a lot of data needs to be grouped together--for instance, they can be used to hold records from a database or to store information about contacts in an address book. In the contacts example, a struct could be used that would hold all of the information about a single contact--name, address, phone number, and so forth.
The format for defining a structure is
For example:
struct example {
int x;
};
struct example an_example; /* Treating it like a normal variable type
except with the addition of struct*/
an_example.x = 33; /*How to access its members */
#include <stdio.h>
struct xampl {
int x;
};
int main()
{
struct xampl structure;
struct xampl *ptr;
structure.x = 12;
ptr = &structure; /* Yes, you need the & when dealing with
structures and using pointers to them*/
printf( "%d\n", ptr->x ); /* The -> acts somewhat like the * when
does when it is used with pointers
It says, get whatever is at that memory
address Not "get what that memory address
is"*/
getchar();
}