16.5 – Pointer to a struct variable

by subbu on September 16, 2014

As we know the struct variable is a set of variables. The address of the struct variable is the address of first member.

#include<stdio.h>
struct box
{
 int b;
 int w;
 int h;
}x;
int main()
{
 printf("Address of struct variable : %p",&x);
 printf("\nAddress of first member : %p",&x.b);
 return 0;
}

Output:
Address of struct variable : 036E
Address of first member : 036E

Here it is proved that, the address of struct variable and the address of the first member are same (036E).

To store the address of a struct variable, the pointer must be of the same struct type. The size of pointer would be the same as any other pointer.

 struct box x={10,20,30};
 struct box *p;
 p=&x;

pointer to struct variable

Now “p” is the pointer to the struct variable “x”, “P” could refer every member of struct variable “x” using indirection operator * (value at)

Here *p refers the total struct variable and by which we can refer individual members as (*p).b, (*p).w and (*p).h

The reason why we place *p in parenthesis is to get more priority to * (value at) operator over . (dot) operator because the actual priority of . (dot) is more than * (value at) operator.

#include<stdio.h>
struct box
{
 int b;
 int w;
 int h;
};
int main()
{
 struct box x={10,20,30};
 struct box *p;
 p=&x;
 printf("Three sides of box: %d\t%d\t%d",(*p).b,(*p).w,(*p).h);
 return 0;
}

Output:
Three sides of box: 10   20    30

Pointer to operator (->)

It is a special operator; even it can be used to access the members of struct variable indirectly.

pointer to operator

#include<stdio.h>
struct box
{
 int b;
 int w;
 int h;
};
int main()
{
 struct box x={10,20,30};
 struct box *p;
 p=&x;
 printf("Three sides of box: %d\t%d\t%d",p->b,p->w,p->h);
 return 0;
}

Output:
Three sides of box: 10   20    30

Specification: Accept the radius of a circle and print the area and circumference using pointer

#include<stdio.h>
#define PI 3.14
struct circle
{
 int rad;
 float area,cir;
}x,*p;               /* declaration of struct variable x and pointer p */
int main()
{
 p=&x;
 printf("Enter the radios:");
 scanf("%d",&(p->rad));
 p->area=PI*(p->rad)*(p->rad);
 p->cir=2*PI*(p->rad);
 printf("Area %f",p->area);
 printf("\nCircumference %f",p->cir);
 return 0;
}

Output:
Enter the radios:15
Area 706.500000
Circumference 94.199997

Pointer representation to struct array

As we know, array is a set of elements of same type stored in adjacent memory allocations and the name of array gives the address of first element, by which we can refer every element of array using address arithmetic *(a+i)

The same way a struct array is a set of struct type instances stored in adjacent memory locations. The name of struct array give the address of first struct instance, by which we can refer every instance in the array using address arithmetic.

struct point
{
 int x;
 int y;
};

struct point a[]={{34,56},{22,12},{45,32}};
struct point *p;
p=a;

pointer arithmetic with struct

Here “p” is another pointer to the array. It refers all the elements as “p” is keep on incremented by 1.

Initially “p” is 65500 hence refers a[0], When “p” is incremented by one then it refers a[1] because the scale factor of “p” is the size of “struct point” hence “p” would be 65504 that is refers next element. The same way pointer “p” could refer every element of array through pointer arithmetic.

for(i=1;i<=3;i++)
{
 printf("x=%d\ty=%d\n",p->x,p->y);
 p++;  /* visiting next element of array */
}

Program

#include<stdio.h>
struct point
{
 int x;
 int y;
};
int main()
{
 struct point a[]={{34,56},{22,12},{45,32}};
 struct point *p;
 int i;
 printf("Coords of all the points..\n");
 /*for(i=0;i<3;i++)
    printf("x=%d\ty=%d\n",a[i].x,a[i].y);*/
 p=a; /* p is another pointer to array */
 for(i=1;i<=3;i++)
 {
  printf("x=%d\ty=%d\n",p->x,p->y);
  p++;  /* visiting next element of array */
 }
 return 0;
}

Output
Coords of all the points..
x=34    y=56
x=22    y=12
x=45    y=32

Previous post:

Next post: