Introduction
A structure is a user-defined data type available in C that allows us to combine data items of different types. It is also known as struct.
Declaration
You can declare your structures in the global scope of your program i.e. outside of all functions similar to prototypes. You can then declare elements of your structures in its scope.
/**
* struct User - A user structure
* @name: First member
* @email: Second member
* @age: Third member
*
* Description: Defines a user structure
*/
struct User
{
char* name;
char* email;
int age;
};
/**
* main - Entry point
*
* Return: Always 0.
*/
int main(void)
{
struct User user;
return (0);
}
Note: A structure should always end in a semicolon.
Elements of a structure are accessed by using the "." symbol.
Example
The following example shows how to access data in a structure:
#include <stdio.h>
/**
* struct User - A user structure
* @name: First member
* @email: Second member
* @age: Third member
*
* Description: Defines a user structure
*/
struct User
{
char* name;
char* email;
int age;
};
/**
* main - Entry point
*
* Return: Always 0.
*/
int main(void)
{
struct User user;
user.name = "Foo Bar";
user.email = "foo@example.com";
user.age = 21;
printf("%s\n", user.name);
printf("%s\n", user.email);
printf("%d\n", user.age);
return (0);
}
The above example will have the following output:
Foo Bar
foo@example.com
21
Pointers to structures
To access elements of a pointer to a structure, you have to dereference the pointer and then access the data using the "." symbol. However, there is a simpler way of doing that, just by using the symbol "->". This symbol dereferences a pointer to the structure and accesses the value of a member of the structure.
Example 1
The following example shows two ways to access elements of a pointer to a structure.
#include <stdio.h>
/**
* struct User - A user structure
* @name: First member
* @email: Second member
* @age: Third member
*
* Description: Defines a user structure
*/
struct User
{
char* name;
char* email;
int age;
};
/**
* main - Entry point
*
* Return: Always 0.
*/
int main(void)
{
struct User user;
struct User *ptr;
ptr = &user;
/* Dereferencing the pointer before accessing
the data with the "." symbol */
(*ptr).name = "Foo Bar";
/* Using the "->" works the same an is much easier */
ptr->email = "foo@example.com";
return (0);
}
Example 2
#include <stdio.h>
#include <stdlib.h>
/**
* struct User - A user structure
* @name: First member
* @email: Second member
* @age: Third member
*
* Description: Defines a user structure
*/
struct User
{
char *name;
char *email;
int age;
};
/**
* struct User new_user - A user structure
* @name: First member
* @email: Second member
* @age: Third member
*
* Description: Defines a user structure
* Return: user structure
*/
struct User *new_user(char *name, char *email, int age)
{
struct User *user;
user = malloc(sizeof(struct User));
if (user == NULL)
return (NULL);
user->name = name;
user->email = email;
user->age = age;
return (user);
}
/**
* main - Entry point
*
* Return: 0 (Success). Otherwise 1.
*/
int main(void)
{
struct User *user;
user = new_user("Foo", "foo@foo.bar", 21);
if (user == NULL)
return (1);
printf("User %s created!\n", user->name);
printf("Email: %s\n", user->email);
printf("Age %d\n", user->age);
return (0);
}
The above example will produce the following output:
User Foo created!
Email: foo@foo.bar
Age: 21
How to use typedef
The C programming provides a keyword called typdef
, which you can use to give a type, a new name. It can be used with structures to define a new data type. Allows us to create aliases for data types.
Syntax
It uses the following syntax:
typedef exiting_name alias_name;
After this declaration, alias_name becomes equivalent to the type you wanted.
Example
The following example uses a typedef to define a new data type for a structure:
/**
* user - Typedef for struct user
*/
typedef struct User
{
char *name;
char *email;
int age;
} user;
/**
* main - Entry point
*
* Return: Always 0.
*/
int main(void)
{
struct User user;
user user2;
return (0);
}
Note: A typedef should always end in a semicolon.
Conclusion
Structures and typedef help us to organize and manage our code more effectively. You should use structures when you want to group related data, and typedef when you want to create aliases for data types to improve code readability and maintainability. These features become useful in larger projects where organization and clarity are crucial.
Happy Coding!