Pointers with Structures in C

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

Overview

C allows programmers to create user-defined data types by grouping data of different types together using struct keywords, such data types are called structures. Like any other data type in C, variables of user-defined structure occupy addresses in a memory block, and pointers can be used to point them. A pointer pointing to a structure is called structure pointer. Structures and pointers in C together help in accessing structure members efficiently.

Structure pointer declaration is similar to declaring a structure variable using the struct keyword followed by the type of structure it will point to. A structure pointer can only hold the address of the structure used at the time of its declaration. Structures and pointers in C together make accessing structure value and passing to functions easier.

Introduction

C allows programmers to create their data type by grouping different types together into one using structures. For example, if we want to store information about our classmates, each student variable should hold information about the student's name, roll number, and grades. No pre-defined data type in C can alone store all this information.

For such cases where we want to store information that no data type can hold, we create our data types using structure to hold the required information.

Different components of a structure are called members for example, in the above case, student name and roll number are members of the structure. Like every other data type, structure variables are stored in memory, and we can use pointers to store their addresses.

Structure pointer points to the address of the structure variable in the memory block to which it points. This pointer can be used to access and change the value of structure members. This way, structures and pointers in C can be used to create and access user-defined data types conveniently.

Before understanding how to use structures and pointers in C together, let us understand how structures are defined and accessed using the variable name.

Syntax to Define a Structure

C struct keyword is used to create a new data type, followed by the structure name. We define different members of the structure inside parenthesis. Once a structure is defined, its name structure_name can be used to declare variables as

To access the value of members of a structure, the dot (.) operator and the structure variable name followed by the member's name are used. For example, if we want the value of member_variable_1 from a structure variable structure_variable syntax will be

structure_variable is the structure variable, and member_variable_1 is one of its members.

Note:

The structure members do not occupy space in memory until they are associated with a structure variable.

Example

Now that we know how structures are declared and accessed let us create a structure User that holds information about user name, his role and his age. Here name, role, age are members of the structure User.

Output

Explanation

Here, we have created a user-defined data type, User using the struct keyword, this structure has three members that are name (string), age (int), and role (string). To store information of two users, we have declared two structure variables user_1 and user_2 of type User and later initialized and accessed their value in the main() function using the variable name and dot (.) operator.

Declare a Structure Pointer

declare a structure pointer in c As shown in the above figure, a structure pointer stores the memory address of a structure variable. This is the reason why in the figure ptr stores the location 3000 inside it, which is the address of the variable student1.

We now know how structures are defined and used in a C code let us see how we can use structures with pointers to access structure variables and their members. Declaration of structure pointer is similar to the declaration of structure variables, and the only difference is that the pointer name is prefixed with an asterisk * symbol.

Syntax

Structure pointer in C is declared using the keyword struct followed by structure name to which the pointer will point to followed by pointer name. A structure pointer can only hold the address of a variable of the same structure type used in its declaration.

This way structures and pointers in C are used together to create a pointer pointing to the structure.

Initialization of Structure Pointer

After a structure pointer is declared, we need to initialize it to a variable before using it. To initialize a variable, we need to provide the address of the structure variable using the & operator.

Also, the structure pointer can be initialized during the time of declaration.

Accessing Structure Member Using Pointer

There are two ways to access the values of structure members using pointers -

1. Using asterisk (*) and dot (.) operator with the structure pointer. 2. Using membership or arrow (->) operator.

Examples

Let us see some examples to understand how we can access structure members using two different approaches.

Example 1: Accessing structure members using the dot operator

Output

Explanation

Here, cp is a pointer that points to the structure variable first_point. This means dereferencing the pointer gives us the content of first_point. Hence, *cp and first_point are functionally identical. To access members of the structure dot operator can be used followed by the member name.

For example, in the example above:

  • (*cp).x refers to member x of first_point.
  • (*cp).y refers to member y of first_point.

Note: Parentheses around the pointer is important because the precedence of dot operator is greater than indirection (*) operator.

Example 2: Accessing structure members using the arrow operator

Another way to access structure members in C is using the (->) operator. Using this way, we don't need an asterisk and dot operator with the pointer. To access members of the structure using (->) operator we write pointer name with -> followed by the name of the member that is

Let us see an example to understand how we can use an arrow operator to access structure members using structures and pointers in C.

Output

Accessing members of the structure using the membership operator on structure pointer makes code more readable when compared to the other approach.

Example 3: Structure pointer in function arguments

Output

Here, we have defined function arguments as structure pointers and when we are creating function calls instead of passing structure variables, we are passing reference of them to function. Because reference of variable is passed to the function any changes made on structure members inside function body will persist outside the function scope.

Conclusion

  • Structures in C allow programmers to create user-defined data types by grouping different defined data types into one. Different individual components in the structure are called members.
  • To create a new structure struct keyword is used and similarly, when a structure variable is created struct keyword is used followed by structure type and variable name.
  • Pointer pointing to a structure variable is called a structure pointer, and structures and pointers in C together can be used to access and change the values of members of the structure they are pointing.
  • Declaring a structure pointer is similar to the declaration of a structure variable. To declare a structure pointer struct keyword is used followed by the structure name and pointer name with an asterisk * symbol.
  • Members of a structure can be accessed from pointers using two ways that are.
    • Using dot and asterisk operator on a pointer.
    • Using arrow operator (->) on a pointer.