- 0x0D. Structures.pdf
- struct (C programming language)
- Documentation: structures
- 0x0D. Typedef and structures.pdf
- typedef
- Programming in C by Stephen Kochan - Chapter 8, Working with Structures p163-189
- The Lost Art of C Structure Packing
In C programming, a structure is a collection of variables that can be of different data types. It allows you to group related data items and organize them into a single unit. You can access each item individually or together as a group.
Structures are commonly used when you want to group data together and pass it as a single entity to functions or modules. They also allow you to create user-defined data types that can be used in your program.
To define a structure, you use the struct
keyword followed by the name of the structure and a set of braces that contains the variables or members of the structure:
struct person {
char name[50];
int age;
float salary;
};
You can then create variables of this structure type and access the members using the dot .
operator:
struct person achraf;
strcpy(achraf.name, "Achraf El Khnissi");
achraf.age = 44;
achraf.salary = 1;
Structures can also be used as function parameters or return types:
struct person get_person() {
struct person p;
strcpy(p.name, "Achraf El Khnissi");
p.age = 44;
p.salary = 1;
return p;
}
typedef
is a keyword in C that allows us to create an alias for an existing data type or structure. It is useful when we want to create a new data type that represents an existing type or structure. Here's an example:
typedef struct {
char name[20];
int age;
} Person;
The above code creates a new data type called Person
that represents the structure struct person
. We can now use the Person
data type to declare variables of this type:
Person achraf;
achraf.age = 44;
strcpy(achraf.name, "Achraf El Khnissi");
typedef
can also be used to create aliases for existing data types. For example, the following code creates an alias for the int
data type:
typedef int Integer;
Integer i = 10; /* i is an int */
0. Django
#include <stdio.h>
#include "dog.h"
/**
* main - check the code
* Return: Always 0.
*/
int main(void)
{
struct dog my_dog;
my_dog.name = "Poppy";
my_dog.age = 3.5;
my_dog.owner = "Bob";
printf("My name is %s, and I am %.1f :) - Woof!\n", my_dog.name, my_dog.age);
return (0);
}
Compile the code this way:
gcc -Wall -pedantic -Werror -Wextra 0-main.c 0-dog.c -o a
Output:
$ ./a
My name is Poppy, and I am 3.5 :) - Woof!
$
1. A dog is the only thing on earth that loves you more than you love yourself
#include <stdio.h>
#include "dog.h"
/**
* main - check the code
* Return: Always 0.
*/
int main(void)
{
struct dog my_dog;
init_dog(&my_dog, "Poppy", 3.5, "Bob");
printf("My name is %s, and I am %.1f :) - Woof!\n", my_dog.name, my_dog.age);
return (0);
}
Compile the code this way:
gcc -Wall -pedantic -Werror -Wextra 1-main.c 1-init_dog.c -o b
Output:
$ ./b
My name is Poppy, and I am 3.5 :) - Woof!
$
2. A dog will teach you unconditional love. If you can have that in your life, things won't be too bad
2. A dog will teach you unconditional love. If you can have that in your life, things won't be too bad
#include <stdio.h>
#include "dog.h"
/**
* main - check the code
* Return: Always 0.
*/
int main(void)
{
struct dog my_dog;
init_dog(&my_dog, "Poppy", 3.5, "Bob");
print_dog(&my_dog);
return (0);
}
Compile the code this way:
gcc -Wall -pedantic -Werror -Wextra 2-main.c 2-print_dog.c 1-init_dog.c -o c
Output:
$ ./c
Name: Poppy
Age: 3.500000
Owner: Bob
$
3. Outside of a dog, a book is a man's best friend. Inside of a dog it's too dark to read
#include <stdio.h>
#include "dog.h"
/**
* main - check the code
* Return: Always 0.
*/
int main(void)
{
dog_t my_dog;
my_dog.name = "Poppy";
my_dog.age = 3.5;
my_dog.owner = "Bob";
printf("My name is %s, I am %.1f :) - Woof!\n", my_dog.name, my_dog.age);
return (0);
}
Compile the code this way:
gcc -Wall -pedantic -Werror -Wextra 3-main.c 3-dog.c -o d
Output:
$ ./d
My name is Poppy, I am 3.5 :) - Woof!
$
4. A door is what a dog is perpetually on the wrong side of
#include <stdio.h>
#include "dog.h"
/**
* main - check the code
* Return: Always 0.
*/
int main(void)
{
dog_t *my_dog;
my_dog = new_dog("Poppy", 3.5, "Bob");
printf("My name is %s, and I am %.1f :) - Woof!\n", my_dog->name, my_dog->age);
return (0);
}
Compiled with:
gcc -Wall -pedantic -Werror -Wextra 4-main.c 4-new_dog.c 3-dog.c -o e
Output:
$ ./e
My name is Poppy, and I am 3.5 :) - Woof!
$
5. How many legs does a dog have if you call his tail a leg? Four. Saying that a tail is a leg doesn't make it a leg
5. How many legs does a dog have if you call his tail a leg? Four. Saying that a tail is a leg doesn't make it a leg
#include <stdio.h>
#include "dog.h"
/**
* main - check the code
* Return: Always 0.
*/
int main(void)
{
dog_t *my_dog;
my_dog = new_dog("Poppy", 3.5, "Bob");
printf("My name is %s, and I am %.1f :) - Woof!\n", my_dog->name, my_dog->age);
free_dog(my_dog);
return (0);
}
Compiled with:
gcc -Wall -pedantic -Werror -Wextra 5-main.c 5-free_dog.c 4-new_dog.c 3-dog.c -o f
Output:
$ valgrind ./f
==22840== Memcheck, a memory error detector
==22840== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==22840== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==22840== Command: ./f
==22840==
My name is Poppy, and I am 3.5 :) - Woof!
==22840==
==22840== HEAP SUMMARY:
==22840== in use at exit: 0 bytes in 0 blocks
==22840== total heap usage: 4 allocs, 4 frees, 1,059 bytes allocated
==22840==
==22840== All heap blocks were freed -- no leaks are possible
==22840==
==22840== For counts of detected and suppressed errors, rerun with: -v
==22840== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
$