Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Leer Defining and Declaring Structs | Introduction to Structs
C Structs
course content

Cursusinhoud

C Structs

C Structs

1. Introduction to Structs
2. Pointers and Structs
3. Structs and Memory
4. Advanced Structs Usage
5. Implementing Data Structures

book
Defining and Declaring Structs

Defining

A structure declaration begins with the keyword "struct".

struct <name_of_struct> 
{
	<type_1> <field_1>;
    <type_2> <field_2>;
            …
    <type_N> <field_N>;
};

<type_1> <field_1> - variables that will make up the structure.

The structure declaration does not take up memory, that is, it is just a drawing, a template.

First way

Once we have declared a structure, we can create a variable of that type using the word struct.

struct <name_of_struct> <name_of_variable>;

Declaring multiple struct variables is the same as declaring regular variables:

struct <name_of_struct> <name_of_variable_1>, <name_of_variable_2>,<name_of_variable_3>;

Second way

This option allows the structure to be instantiated immediately after it is defined.

struct <name_of_struct> 
{
	<type_1> <field_1>;
    <type_2> <field_2>;
            …
    <type_N> <field_N>;	
} <name_of_variable>; // defining of variable

In this way it is convenient to create several variables at once:

struct <name_of_struct> 
{
	<type_1> <field_1>;
    <type_2> <field_2>;
            …
    <type_N> <field_N>;	
} <name_of_variable_1>, <name_of_variable_2>;

Spoiler method

The use of the typedef keyword is also used in creating structures:

typedef struct {
    <type_1> <field_1>
    <type_2> <field_1>
} <name_of_struct>;

<name_of_struct> <name_of_variable>;

Note

The use of the "typedef" keyword will be covered later in this course.

Initialization

Initializing a structure == initializing a variable of a new data type

To initialize a structure, you must use curly braces {...}:

struct <name of struct> 
{
	<type_1> <field_1>;
    <type_2> <field_2>;
            …
    <type_N> <field_N>;
};

// initialization of struct-variable
struct <name of struct> <name_of_variable> = {field_1, field_2, field_N};

In this case, the fields will be assigned in order and there is no need to indicate the data type of each field, because this has already been done in the blueprint of the structure.

question mark

Select the option with the correct structure declaration:

Select the correct answer

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 1. Hoofdstuk 3

Vraag AI

expand

Vraag AI

ChatGPT

Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.

course content

Cursusinhoud

C Structs

C Structs

1. Introduction to Structs
2. Pointers and Structs
3. Structs and Memory
4. Advanced Structs Usage
5. Implementing Data Structures

book
Defining and Declaring Structs

Defining

A structure declaration begins with the keyword "struct".

struct <name_of_struct> 
{
	<type_1> <field_1>;
    <type_2> <field_2>;
            …
    <type_N> <field_N>;
};

<type_1> <field_1> - variables that will make up the structure.

The structure declaration does not take up memory, that is, it is just a drawing, a template.

First way

Once we have declared a structure, we can create a variable of that type using the word struct.

struct <name_of_struct> <name_of_variable>;

Declaring multiple struct variables is the same as declaring regular variables:

struct <name_of_struct> <name_of_variable_1>, <name_of_variable_2>,<name_of_variable_3>;

Second way

This option allows the structure to be instantiated immediately after it is defined.

struct <name_of_struct> 
{
	<type_1> <field_1>;
    <type_2> <field_2>;
            …
    <type_N> <field_N>;	
} <name_of_variable>; // defining of variable

In this way it is convenient to create several variables at once:

struct <name_of_struct> 
{
	<type_1> <field_1>;
    <type_2> <field_2>;
            …
    <type_N> <field_N>;	
} <name_of_variable_1>, <name_of_variable_2>;

Spoiler method

The use of the typedef keyword is also used in creating structures:

typedef struct {
    <type_1> <field_1>
    <type_2> <field_1>
} <name_of_struct>;

<name_of_struct> <name_of_variable>;

Note

The use of the "typedef" keyword will be covered later in this course.

Initialization

Initializing a structure == initializing a variable of a new data type

To initialize a structure, you must use curly braces {...}:

struct <name of struct> 
{
	<type_1> <field_1>;
    <type_2> <field_2>;
            …
    <type_N> <field_N>;
};

// initialization of struct-variable
struct <name of struct> <name_of_variable> = {field_1, field_2, field_N};

In this case, the fields will be assigned in order and there is no need to indicate the data type of each field, because this has already been done in the blueprint of the structure.

question mark

Select the option with the correct structure declaration:

Select the correct answer

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 1. Hoofdstuk 3
some-alt