Sample


[br]10 [br]Arrays are set of elements of the same type, but what if we wanted to create a set of <strong>elements of different types</strong>? Then we can use <strong>structures</strong>. A structure is a <strong>user-defined data type</strong>, that means it's a data type created by the user - <em>while types such as int and char already exist by default</em>. When you create a structure, you specify a name - <em>that can be used to define variables</em> - and its elements. Each element has got a type and a name, like every other variable. So, if you create a structure named ABC with two elements, an int named X and a char named Y, and you define two variables of that new type, VAR1 and VAR2, <strong>both variables will have those elements</strong>, but VAR1's X <strong>may have a different value</strong> than VAR2's, as well as <strong>two int variables don't have to share the same value</strong>. <strong>In short</strong>, a structure is a new data type and it works like any other data type. It isn't an array by itself, but it defines a <strong>blueprint</strong> that turns variables of that type in something similar to an array. <strong>Structure are defined</strong> as follows: [crayon-5d58af7c515a9671410129/] and every time you define a variable you <strong>must specify</strong> that its type is a structure: [crayon-5d58af7c515b2674342395/] <strong>Initialising</strong> a structure variable is similar to initialising an array - the value of each element in the order in which they are defined between curly brackets: [crayon-5d58af7c515b5953411780/] <hr> OK, anyway. You know that each variable has its own reserved amount of memory. What if more variables <strong>shared the same memory space</strong> or part of it? We could interpret a byte in <strong>different ways</strong>. We could use a 4-bytes number as a string of 4 characters. This is exactly what <strong>unions</strong> do. Unions are created similarly to structures: [crayon-5d58af7c515b8978163790/] but all the elements share the same space in the memory or parts of it if they are of different types. <hr> Finally, the most important question: is it possible to <strong>not having to write struct or union</strong> every time we want to define a new variable? Yes, it is, thanks to <strong>typedef</strong>. The typedef keyword adds a new name to a data type and it can be used with <strong>any type</strong>, not only structures and unions. We can, for example, shorten <em>char</em> to <em>ch</em> or assign to a structure a new name that won't require the <strong>struct</strong> keyword every time we define a new variable. [crayon-5d58af7c515bb234275882/] You can create a structure called <em>Record</em>: [crayon-5d58af7c515bf936837342/] and use <strong>typedef</strong> to avoid usign the struct keyword in variable declarations: [crayon-5d58af7c515c1813314304/] Or we can fuse <strong>struct and typedef</strong> together, since we wouldn't use the name <em>Rec</em> anyway: [crayon-5d58af7c515c4883767992/] You can access the elements of a structure in two ways: <ul> <li><strong>A dot</strong> (<code>var.element</code>) for variables;</li> <li><strong>-></strong> (<code>pointer.element</code>) for pointers to structure variables.</li> </ul> [br]https://itszephyro.com/blog/learn-c/structures_user_defined_types/[br]#include <stdio.h> int main() { typedef struct{ char name[20]; int age; int office; } Employee; Employee a = {"Mark", 32, 2}; Employee b = {"Bob", 53, 1}; printf("%s is %d years old and he works at the office n. %d\n", a.name, a.age, a.office); printf("%s is %d years old and he works at the office n. %d\n", b.name, b.age, b.office); } [br]

[br]10
[br]Arrays are set of elements of the same type, but what if we wanted to create a set of elements of different types?

Then we can use structures.

A structure is a user-defined data type, that means it’s a data type created by the user – while types such as int and char already exist by default.

When you create a structure, you specify a name – that can be used to define variables – and its elements.

Each element has got a type and a name, like every other variable.

So, if you create a structure named ABC with two elements, an int named X and a char named Y, and you define two variables of that new type, VAR1 and VAR2, both variables will have those elements, but VAR1’s X may have a different value than VAR2’s, as well as two int variables don’t have to share the same value.

In short, a structure is a new data type and it works like any other data type. It isn’t an array by itself, but it defines a blueprint that turns variables of that type in something similar to an array.

Structure are defined as follows:

and every time you define a variable you must specify that its type is a structure:

Initialising a structure variable is similar to initialising an array – the value of each element in the order in which they are defined between curly brackets:


OK, anyway. You know that each variable has its own reserved amount of memory.

What if more variables shared the same memory space or part of it?

We could interpret a byte in different ways. We could use a 4-bytes number as a string of 4 characters.

This is exactly what unions do.

Unions are created similarly to structures:

but all the elements share the same space in the memory or parts of it if they are of different types.


Finally, the most important question: is it possible to not having to write struct or union every time we want to define a new variable?

Yes, it is, thanks to typedef.

The typedef keyword adds a new name to a data type and it can be used with any type, not only structures and unions.

We can, for example, shorten char to ch or assign to a structure a new name that won’t require the struct keyword every time we define a new variable.

You can create a structure called Record:

and use typedef to avoid usign the struct keyword in variable declarations:

Or we can fuse struct and typedef together, since we wouldn’t use the name Rec anyway:

You can access the elements of a structure in two ways:

  • A dot (var.element) for variables;
  • -> (pointer.element) for pointers to structure variables.

[br]https://itszephyro.com/blog/learn-c/structures_user_defined_types/[br]#include

int main() {
typedef struct{
char name[20];
int age;
int office;
} Employee;

Employee a = {“Mark”, 32, 2};
Employee b = {“Bob”, 53, 1};

printf(“%s is %d years old and he works at the office n. %d\n”, a.name, a.age, a.office);
printf(“%s is %d years old and he works at the office n. %d\n”, b.name, b.age, b.office);
}
[br]

To learn more, visit: