C
07-Structures

Structures

The purpose of a structure in C programming is to allow programmers to define custom data types by grouping together various data types under one name. This allows for the creation of more complex data structures that can hold different types of data. Structures provide a way to organize and manage related data efficiently.

Declaration

struct Tag
{
    [type] [identifier];
    // ... other types
};
  • struct keyword is used to declare a structure.
  • Tag is the name of the structure.
  • Inside the curly braces you list the types of data that will be in your structure. Each data type in the structure is called a "member" or "field".
  • type is the data type of the member. It can be any valid C data type, like int, float, or even other structures.
  • identifier is the name of the member that you will use to access the member's value.

Initialization

To set up a new structure variable with starting values in C, we use braces with a list of values inside. These values are separated by commas and must be in the same order as the members in the structure. This is how we do it:

struct Tag identifier = { value, ... , value };

Member Acces

To access a member of an object of a structure, we use the dot operator (.). Dot notation takes the form:

object.member

To access an element of an array member, we use subscript notation

object.member[index]