Register Now

Login

Lost Password

Enter your email to reset your password.

BY Author

C pointers & structure

1) Pointers to structure variable

It is possible to create a pointer that can point to the structure variable.

C Programming Language Pointer to Structure
Declaration

struct structure_name *pointer_name;

Example

struct Employee
{
    int id_no;
    char name[50];
    float salary;
};

int main()
{
    struct Employee *ptr;
}

Assigning structure variable to pointer variable

struct structure_name *pointer_name = &structure_variable;

                                                                       OR

struct structure_name *pointer_name;
pointer_name = &structure_variable;

We can access structure members through pointer using dot(.) operator or arrow(->) operator.

1) Using dot operator

(*pointer_name).structure_member

2) Using arrow operator

pointer_name->structure_member

Examples using dot operator

Example No. 1

#include <stdio.h>

struct Employee
{
    int id_no;
    char name[50];
    float salary;
};

int main()
{
    struct Employee emp = {11,"ROBIN",66667.58};
    struct Employee *ptr = &emp; // pointer to structure variable
    printf("\n ID NO. = %d",(*ptr).id_no);
    printf("\n Name = %s",(*ptr).name);
    printf("\n Salary = %f",(*ptr).salary);
	
    return 0;
}

Output

ID NO. = 11
Name = JOHN
Salary = 55000.648438
City = New York
ZIP = 10001

Example No. 2

#include <stdio.h>

struct Employee
{
    int id_no;
    char name[50];
    float salary;
};

int main()
{
    struct Employee emp;
    struct Employee *ptr = &emp;
	
    printf("\n Enter ID NO. of employee = ");
    scanf("%d",&(*ptr).id_no);
    printf("\n Enter Name of employee = ");
    scanf("%s",(*ptr).name);
    printf("\n Enter Salary of employee = ");
    scanf("%f",&(*ptr).salary);
	
    printf("\n ID NO. = %d",(*ptr).id_no);
    printf("\n Name = %s",(*ptr).name);
    printf("\n Salary = %f",(*ptr).salary);
	
    return 0;
}

Output

Enter ID NO. of employee = 4

Enter Name of employee = CARRY

Enter Salary of employee = 76878.98

ID NO. = 4
Name = CARRY
Salary = 76878.976563

Example using arrow operator

Example

#include <stdio.h>

struct Employee
{
    int id_no;
    char name[50];
    float salary;
};

int main()
{
    struct Employee emp = {8,"WARREN",56798.34};
    struct Employee *ptr = &emp;
    printf("\n ID NO. = %d",ptr->id_no);
    printf("\n Name = %s",ptr->name);
    printf("\n Salary = %f",ptr->salary);
	
    return 0;
}

Output

ID NO. = 8 
Name = WARREN 
Salary = 56798.339844

Pointer as a structure member

Example No. 1

#include<stdio.h>

struct Employee
{
    int id_no;
    char name[50];
    float salary;
    int *p1;
    char *p2;
    float *p3;
};

int main()
{
    struct Employee emp = {13,"SACHIN",98657.45};
	
    emp.p1 = &emp.id_no;
    emp.p2 = emp.name;
    emp.p3 = &emp.salary;

    printf("\n ID NO. = %d",*emp.p1);
    printf("\n Name = %s",emp.p2);
    printf("\n SALARY = %f",*emp.p3);
	
    return 0;
}

Output

ID NO. = 13
Name = SACHIN
SALARY = 98657.453125

Example No. 2

#include<stdio.h>

struct Employee
{
    int id_no;
    char name[50];
    float salary;
    int *p1;
    char *p2;
    float *p3;
};

int main()
{
    struct Employee emp;
	
    emp.p1 = &emp.id_no;
    emp.p2 = emp.name;
    emp.p3 = &emp.salary;
	
    printf("\n Enter ID NO. of employee = ");
    scanf("%d",&(*emp.p1));
    printf("\n Enter Name of employee = ");
    scanf("%s",&(*emp.p2));
    printf("\n Enter Salary of employee = ");
    scanf("%f",&(*emp.p3));
	
    printf("\n ID NO. = %d",*emp.p1);
    printf("\n Name = %s",emp.p2);
    printf("\n SALARY = %f",*emp.p3);
	
    return 0;
}

Output

Enter ID NO. of employee = 23

Enter Name of employee = JACK

Enter Salary of employee = 99898.99

ID NO. = 23
Name = JACK
SALARY = 99898.992188

 

Leave a reply