Pointer to a Structure in C

We have already learned that a pointer is a variable which points to the address of another variable of any data type like int, char, float etc. Similarly, we can have a pointer to structures, where a pointer variable can point to the address of a structure variable. Here is how we can declare a pointer to a structure variable.

This declares a pointer ptr_dog that can store the address of the variable of type struct dog. We can now assign the address of variable spike to ptr_dog using & operator.

Now ptr_dog points to the structure variable spike.

Accessing members using Pointer

There are two ways of accessing members of structure using pointer:

  1. Using indirection (*) operator and dot (.) operator.
  2. Using arrow (->) operator or membership operator.

Let’s start with the first one.

Using Indirection (*) Operator and Dot (.) Operator

At this point ptr_dog points to the structure variable spike, so by dereferencing it we will get the contents of the spike. This means spike and *ptr_dog are functionally equivalent. To access a member of structure write *ptr_dog followed by a dot(.) operator, followed by the name of the member. For example:

(*ptr_dog).name – refers to the name of dog
(*ptr_dog).breed – refers to the breed of dog

and so on.

Parentheses around *ptr_dog are necessary because the precedence of dot(.) operator is greater than that of indirection (*) operator.

Using arrow operator (->)

The above method of accessing members of the structure using pointers is slightly confusing and less readable, that’s why C provides another way to access members using the arrow (->) operator. To access members using arrow (->) operator write pointer variable followed by -> operator, followed by name of the member.

and so on.

Here we don’t need parentheses, asterisk (*) and dot (.) operator. This method is much more readable and intuitive.

We can also modify the value of members using pointer notation.

Here we know that the name of the array (ptr_dog->name) is a constant pointer and points to the 0th element of the array. So we can’t assign a new string to it using assignment operator (=), that’s why strcpy() function is used.

In the above expression precedence of arrow operator (->) is greater than that of prefix decrement operator (--), so first -> operator is applied in the expression then its value is decremented by 1.

The following program demonstrates how we can use a pointer to structure.

Expected Output:

How it works:

In lines 3-9, we have declared a structure of type dog which has four members namely name, breed, age and color.

In line 13, a variable called my_dog of type struct dog is declared and initialized.

In line 14, a pointer variable ptr_dog of type struct dog is declared.

In line 15, the address of my_dog is assigned to ptr_dog using & operator.

In lines 17-20, the printf() statements prints the details of the dog.

In line 23, a new name is assigned to ptr_dog using the strcpy() function, because we can’t assign a string value directly to ptr_dog->name using assignment operator.

In line 26, the value of ptr_dog->age is incremented by 1 using postfix increment operator. Recall that postfix ++ operator and -> have the same precedence and associates from left to right. But since postfix ++ is used in the expression first the value of ptr_dog->age is used in the expression then it’s value is incremented by 1.

1 thought on “Pointer to a Structure in C

Leave a Comment