Examples Using Pointers in C

  1. Home
  2. C Programming Tutorial
  3. Pointer to a Structure in C

Pointer to a Structure in C

Last updated on July 27, 2020


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.

                      1  2  3  4  5  6  7  8  9 10 11 12
                                            struct                      dog                      {                      char                      name                      [                      10                      ];                      char                      breed                      [                      10                      ];                      int                      age                      ;                      char                      color                      [                      10                      ];                      };                      struct                      dog                      spike                      ;                      // declaring a pointer to a structure of type struct dog                      struct                      dog                      *                      ptr_dog                    

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.

                                            ptr_dog                      ->                      name                      // refers to the name of dog                      ptr_dog                      ->                      breed                      // refers to the breed of dog                    

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.

                        strcpy            (            ptr_dog            ->            name            ,            "new_name"            );          

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.

                      1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
                                            #include                      <stdio.h>                                            struct                      dog                      {                      char                      name                      [                      10                      ];                      char                      breed                      [                      10                      ];                      int                      age                      ;                      char                      color                      [                      10                      ];                      };                      int                      main                      ()                      {                      struct                      dog                      my_dog                      =                      {                      "tyke"                      ,                      "Bulldog"                      ,                      5                      ,                      "white"                      };                      struct                      dog                      *                      ptr_dog                      ;                      ptr_dog                      =                      &                      my_dog                      ;                      printf                      (                      "Dog's name: %s                      \n                      "                      ,                      ptr_dog                      ->                      name                      );                      printf                      (                      "Dog's breed: %s                      \n                      "                      ,                      ptr_dog                      ->                      breed                      );                      printf                      (                      "Dog's age: %d                      \n                      "                      ,                      ptr_dog                      ->                      age                      );                      printf                      (                      "Dog's color: %s                      \n                      "                      ,                      ptr_dog                      ->                      color                      );                      // changing the name of dog from tyke to jack                      strcpy                      (                      ptr_dog                      ->                      name                      ,                      "jack"                      );                      // increasing age of dog by 1 year                      ptr_dog                      ->                      age                      ++                      ;                      printf                      (                      "Dog's new name is: %s                      \n                      "                      ,                      ptr_dog                      ->                      name                      );                      printf                      (                      "Dog's age is: %d                      \n                      "                      ,                      ptr_dog                      ->                      age                      );                      // signal to operating system program ran fine                      return                      0                      ;                      }                    

Expected Output:

                      Dog's name: tyke Dog's breed: Bulldog Dog's age: 5 Dog's color: white  After changes  Dog's new name is: jack Dog's age is: 6                    

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.



Ezoic

Examples Using Pointers in C

Source: https://overiq.com/c-programming-101/pointer-to-a-structure-in-c/

0 Response to "Examples Using Pointers in C"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel