Structures (also called structs) are a way to group several related variables into one place. Each variable in the structure is known as a member of the structure.
Unlike an array, a structure can contain many different data types (int, float, char, etc.).
You can create a structure by using the struct
keyword and declare each of its members inside curly braces:
struct MyStructure { // Structure declaration
int myNum; // Member (int variable)
char myLetter; // Member (char variable)
}; // End the structure with a semicolon
To access the structure, you must create a variable of it.
Use the struct
keyword inside the main()
method, followed by the name of the structure and then the name of the structure variable:
Create a struct variable with the name "s1":
struct myStructure {
int myNum;
char myLetter;
};
int main() {
struct myStructure s1;
return 0;
}
To access members of a structure, use the dot syntax (.
):
// Create a structure called myStructure
struct myStructure {
int myNum;
char myLetter;
};
int main() {
// Create a structure variable of myStructure called **s1**
struct myStructure s1;
// Assign values to members of s1
s1.myNum = 13;
s1.myLetter = 'B';
// Print values
printf("My number: %d\n", s1.myNum);
printf("My letter: %c\n", s1.myLetter);
return 0;
}
Now you can easily create multiple structure variables with different values, using just one structure:
The C language contains the typedef
keyword to allow users to provide alternative names for the primitive (e.g., int) and user-defined (e.g struct) data types.
Remember, this keyword adds a new name for some existing data type but does not create a new type.
Using typedef struct
results in a cleaner, more readable code, and saves the programmer keystrokes. However, it also leads to a more cluttered global namespace which can be problematic for large programs.
You can use typedef to give a name to your user defined data types as well. For example, you can use typedef with structure to define a new data type and then use that data type to define structure variables directly as follows −
typedef struct student_structure{
char* name;
char* surname;
int year_of_birth;
} student;
Define a new type struct dog
with the following elements:
name
, type =char *
age
, type =float
owner
, type =char *
Write a function that initialize a variable of type struct dog
- Prototype:
void init_dog(struct dog *d, char *name, float age, char *owner);
2. A dog will teach you unconditional love. If you can have that in your life, things won't be too bad
Write a function that prints a struct dog
- Prototype:
void print_dog(struct dog *d);
- Format: see example bellow
- You are allowed to use the standard library
- If an element of
d
isNULL
, print(nil)
instead of this element. (ifname
isNULL
, printName: (nil)
) - If
d
isNULL
print nothing.
Define a new type dog_t
as a new name for the type struct dog
.
Write a function that creates a new dog.
- Prototype:
dog_t *new_dog(char *name, float age, char *owner);
- You have to store a copy of
name
andowner
- Return
NULL
if the function fails
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
Write a function that frees dogs.
- Prototype:
void free_dog(dog_t *d);
[solution](5-free_dog.c
)