Pointer to Structure and Union

In this topic, we are going to learn the syntax and the use of pointers to structure and union. Pointer to structure and union is the same as a pointer to a simple variable. Working and purpose of structure and union will remain the same. When we want to store the address of a specific structure or union or we want to point to any specific structure or union then we need to make a pointer for both.

 Syntax of pointer to a structure

Syntax to make a pointer of structure and syntax to access structure members using a pointer is written below.

Defining :

struct  <pointer data type which is same as structure data type>  *<pointer name> =&<structure variable name>;

Access structure member using pointer:

(*<pointer name>).<member name of structure> //Syntax 1

OR

<pointer name>-><member name of structure> //syntax 2

To understand these syntaxe, let us take an example program.

Example program to understand how to make a pointer to a structure and access structure member using pointer

In the below example, there is a structure named str and it is having an int variable ‘a’ which is having value 1 and a float variable ‘b’ having value 2.5. Structure variable name is ‘s’ and a pointer to that structure variable ‘s’ is ‘p’. We have used both syntaxe of accessing structure members using a pointer. Syntax 1 is used to print the value of ‘a’ and syntax 2 is used to print the value of ‘b’.

#include<stdio.h>
#include<conio.h>
struct str
{ 
  int a;
  float b;
};

void main()
{
  struct str s;
  struct str *p=&s;
  clrscr();
  s.a=1;
  printf("a = %d \n",(*p).a);   //syntax 1
  s.b=2.5;
  printf("b = %f ",p->b);   //syntax 2
  getch();
}

Output:

Syntax of pointer to a union

Syntax to make a pointer of union and syntax to access union members using a pointer is written below.

Defining :

union  <pointer data type which is same as union data type>  *<pointer name> =&< union variable name>;

Access structure member using pointer:

(*<pointer name>).<member name of union> //Syntax 1

OR

<pointer name>-><member name of union> //syntax 2

To understand these syntax, let us take an example program.

Example program to understand how to make pointer to union and access union member using pointer

In the below example, there is a union named un and it is having an int variable ‘a’ which is having value 1 and a float variable ‘b’ having value 2.5. Union variable name is ‘s’ and a pointer to that union variable ‘s’ is ‘p’. We have used both syntaxes of accessing union members using a pointer. Syntax 1 is used to print the value of ‘a’ and syntax 2 is used to print the value of ‘b’.

#include<stdio.h>
#include<conio.h>
union un
{
 int a;
  float b;
};

void main()
{
  union un s;
  union un *p=&s;
  clrscr();
  s.a=1;
  printf("a = %d \n",(*p).a); //syntax 1
  s.b=2.5;
  printf("b = %f ",p->b);  //syntax 2
  getch();
}

Output:

Learning from this blog:

  1. Meaning of pointer to a structure.
  2. Syntax to make a pointer to structure.
  3. Meaning of pointer to the union.
  4. Syntax to make a pointer to union.
  5. Methods to access structure/ union members using pointers.
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments