A structure holds data that logically belong together. E.g. a person would have a name and an age and we'd always need to handle this information together. Then we can define a Person structure. For ease of use, we can also create a type definition: TPerson.
The code would look like this in C:
#include <stdio.h>
struct Person
{
char name[64];
unsigned char age;
};
typedef struct Person TPerson;
void PrintPerson(TPerson person)
{
printf("Name: %s\n", person.name);
printf("Age: %d\n", person.age);
}
int main()
{
struct Person person = { "John Doe", 30 };
PrintPerson(person);
return 0;
}One can also unify the structure definition and the type definition by creating an anonymous structure and assigning a name to it via typedef:
typedef struct
{
char name[64];
unsigned char age;
} Person;Structures only hold data, no functionality. For functions, you'd need a class, which is a C++ feature (and other object-oriented languages).