Pointer to Structure in C
Notes:
Pointer to Structure in C Programming Language:
- Pointer is a variable which holds the address of another memory location
Structure:
- Structure is a collection of logically related similar or different type of data elements
- To represent any real world entity inside a C program, we take help of structure construct
Ex:
Mathematical Application – Point, Rectangle, Circle etc.
College Management System Application – Student, Faculty, Subject etc.
Syntax for defining a Structure:
- Definition of a structure is just a template or a blue print for structure variables
// defining a structure
struct StructureName
{
data_type variableName1;
data_type variableName2;
…
data_type variableNameN;
};
EX: // defining a Student structure
struct Student
{
int rollnum;
char *name;
float marks;
};
Syntax for declaring and initializing a structure variable:
- Once we define a structure we can create n number of structure variables of it
// declaring and initializing a structure variable
struct StructureName structureVariableName =
{value 1,value2, … valueN};
EX:
struct Student student1 =
{1,”Suresh”,76.5};
struct Student student2 =
{2,”Ramesh”,86.5};
Syntax for accessing members of a structure variable using structure variable name:
- To access members of a structure variable using structure variable name; we take help of dot or member access operator
// accessing a member of a structure variable
structureVariableName.membername
EX:
printf(“%d\n”,student1.rollnum); // 1
printf(“%s\n”,student1.name); // Suresh
printf(“%f\n”,student1.marks); // 76.5
Pointer to a structure:
- is a variable which holds address of a structure variable
Syntax for declaring and initializing a pointer to a structure variable:
// declaring and initializing a pointer to a structure variable
struct structureName *pointerVariableName = &structureVariableName;
EX:
struct Student *ptrToStudent 1= &student1;
Syntax for accessing members of a structure variable using pointer variable name:
- To access members of a structure variable using pointer variable name we take help of arrow or member access operator
// accessing a member of a structure variable
pointerVariableName->membername
EX:
printf(“%d\n”, ptrToStudent1->rollnum); // 1
printf(“%s\n”, ptrToStudent1->name); // Suresh
printf(“%f\n”, ptrToStudent1->marks); // 86.5
Example Code:
#include <stdio.h>
int main()
{
struct Student
{
int rollnum;
char *name;
float marks;
};
struct Student student1 = {1, "Suresh", 76.5};
struct Student *ptrToStudent1 = &student1;
printf("student1 rollnum = %d\n",student1.rollnum); // 1
printf("student1 name = %s\n",student1.name); // Suresh
printf("student1 marks = %.1f\n",student1.marks); // 76.5
printf("\n");
printf("student1 rollnum = %d\n",ptrToStudent1->rollnum); // 1
printf("student1 name = %s\n",ptrToStudent1->name); // Suresh
printf("student1 marks = %.1f\n",ptrToStudent1->marks); // 76.5
printf("\n\n");
student1.marks = 86.5;
printf("student1 rollnum = %d\n",student1.rollnum); // 1
printf("student1 name = %s\n",student1.name); // Suresh
printf("student1 marks = %.1f\n",student1.marks); // 86.5
printf("\n");
printf("student1 rollnum = %d\n",ptrToStudent1->rollnum); // 1
printf("student1 name = %s\n",ptrToStudent1->name); // Suresh
printf("student1 marks = %.1f\n",ptrToStudent1->marks); // 86.5
printf("\n\n");
ptrToStudent1->marks = 76.5;
printf("student1 rollnum = %d\n",student1.rollnum); // 1
printf("student1 name = %s\n",student1.name); // Suresh
printf("student1 marks = %.1f\n",student1.marks); // 76.5
printf("\n");
printf("student1 rollnum = %d\n",ptrToStudent1->rollnum); // 1
printf("student1 name = %s\n",ptrToStudent1->name); // Suresh
printf("student1 marks = %.1f\n",ptrToStudent1->marks); // 76.5
printf("\n\n");
return 0;
}