×

Structure Pointer in C

Structure Pointer in C

In the C programming language, a structure pointer is defined as a pointer that points to the memory block address that stores a structure. Like C standard language consist of an array of integers, an array of pointers, and so on, we can also have an array of structure variables. To use the array of structure variables efficiently, we can use pointers of structure type. A programmer can also have a pointer to a single structure variable, but it is mainly used when dealing with an array of structure variables.

Structures can be accessed in two ways:

  1. Using a normal structure variable
  2. Using a pointer variable.

A dot (.) operator is used to access the data using a normal structure variable, while arrow (->) is used to access the data using a pointer variable.

E.g.:

 #include <stdio.h>
 #include <conio.h>
 #include <string.h>
 struct point
 {
 int value;
 };
 int main()
 {
 struct point p;
 struct point *ptr = &s;
 return 0;
 } 

In the above code, s is an instance of struct point, and ptr is a struct pointer because it will store the structure’s address.

  •  
 #include <stdio.h>
 #include <conio.h>
 #include <string.h>
 struct point
 {
 int x; //declaration of the structure
 int y;
 };
 struct rect //structure declaration for a rectangle function program.
 {
 struct point left; //an object left is declared along with a point
 struct point right; // an object right is declared along with a point
 };
 void areaOfRectangle (struct rect r) //function to calculate the area of a rectangle
 {
 int area = (r.right.x - r.left.x) * ( r.right.y - r.left.y);
             /* finding the area of a rectangle using variables of point structure where
             the variables of the point structure is being accessed by left and right
             objects*/
 printf (“%d”,area);
 }
 int main()
 {
 struct rect r = { {0, 0}, {1, 1}}; //initialize the variable r with sides of a rectangle
 areaOfRectangle(r);
 return 0;
 } 

Output:

1

  •  
 #include <stdio.h>
 #include <conio.h>
 #include <string.h>
 struct student
 {
 int usn;
 char name[20];
 float percentage;
 int phone;
 char address[50];
 };
 int main ()
 {
 int i;
 struct student detail1 = {1, “Alex”, 97.2, 992880293, “No. 12 church street”};
 struct student *ptr;
 ptr = &detail1;
 printf (“ The details of student 1 are: \n”);
 printf (“USN of the student 1 is: %d \n”, ptr->id);
 printf (“Name of the student 1 is: %s \n”, ptr->name);
 printf (“Percentage of the student 1 is: %f \n”, ptr->percentage);
 printf (“Phone number of the student 1 is: %d \n”, ptr->phone);
 printf (“Address of the student 1 is: %s \n”, ptr->address);
 return 0;
 } 

Output:

 The details of student 1 are:
 USN of the student 1 is: 1
 Name of the student 1 is: Alex
 Percentage of the student 1 is: 97.2
 Phone number of the student 1 is: 992880293
 Address of the student 1 is: No. 12 church street. 

The above code represents the way to store details of a student, which is present in a structure; this structure contains all the information of students such as their USN (University Seat Number), name, percentage, phone number, and address. A programmer’s requirement is to create a method to write the information into the structure. You can also write the memory dynamically, that is, can perform dynamic memory allocation. For efficiency, a pointer to a structure is generally passed to the functions. The members of structures passed within the functions can be accessed to perform dereferencing the structure pointer and then selecting a member using a dot operator (.). It will be very strenuous to dereference the structure pointer every time it is used. This is why the C standard provides a special pointer operator arrow (->) to access the member of a structure pointed by the pointer variable. The arrow operator is a combination of a minus symbol (-) followed by a greater-than symbol (>).

Dynamic memory allocation of structure pointer can be done in the following way:

Sometimes the number of struct variables declared may be insufficient. Hence memory needs to be allocated during run-time.

E.g.:

 #include <stdio.h>
 #include <conio.h>
 #include <stdlib.h>
 struct student
 {
 int usn;
 char name[20];
 float percentage;
 int phone;
 char address[50];
 };
 int main()
 {
 struct student *ptr;
 int i, n;
 printf (“Enter the number of students:”);
 scanf (“%d”, &n); //allocating memory for ‘n’ students
 ptr = (struct student*) malloc(n * sizeof(struct student));
 for (i = 0; i < n; ++i)
 {
 printf (“Enter the USN of the first student:”);
 scanf (“%d”, &(ptr+i)->usn );
 printf (“Enter the name of the first student:”);
 scanf (“%s”, &(ptr+i)->name);
 printf (“Enter the percentage of the first student:”);
 scanf (“%f”, &(ptr+i)->percentage);
 printf (“Enter the phone of the first student:”);
 scanf (“%d”, &(ptr+i)->phone);
 printf (“Enter the address of the first student:”);
 scanf (“%s”, &(ptr+i)->address);
 }
 printf (“Details of students are: \n”);
 for (i = 0; i < n; i++)
             printf (“USN: %d \t Name: %d \t Percentage: %d \t Phone number: %d \t Address: %d \n”, (ptr+i)->usn, (ptr+i)->name, (ptr+i)->percentage, (ptr+i)->phone, (ptr+i)->address);
 return 0;
 } 

Output:

 Enter the USN of the first student: 2
 Enter the name of the first student: Beth
 Enter the percentage of the first student: 95.33
 Enter the phone number of the first student: 8388422
 Enter the address of the first student: No. 15 church street
 Details of students are:
 USN: 1 Name: Alex Percentage:97.2 Phone number: 72779392 Address:No. 12 church street.
 USN: 2 Name: Bev Percentage:95.33 Phone number: 8388422 Address:No. 15 church street. 

Related Topics

Calendar application in C

Introduction to the calendar application We all are familiar with the calendar. It plays a crucial role in our daily life. We run toward the calendar whenever we want to know...

6 minutes read.

Runtime vs compile time in C

In the C programming language, the often used terms in every step consist of compile time and runtime. The compile time is referred to the source code that will be...

4 minutes read.

C –Structure

C structure is a collection of different types of data that is grouped together. It is used to represent records.Structkeyword is used to create a structure. Each element of a...

1 minute read.

Format Specifier in C

Format Specifier in C Format specifiers can be described as an operator that is used to print the data referred by any object or variable in combination with the printf ()...

3 minutes read.

Storage class in C

Storage class in C defines the scope, the visibility, and the lifetime of variables and functions. In other words, storage classes are used to describe the features of variables and...

3 minutes read.

Return array from function in C

Return array from function in C C programming does not require the return to a function of a whole array as an argument. However, you can return a pointer to an array without...

2 minutes read.

Advantages of Dynamic Memory Allocation in C

Dynamic Memory Allocation Dynamic memory allocation is a process of assigning or allocating memory to a variable during the execution of a program. Dynamic memory allocation provides storage according to the...

3 minutes read.

How to use floor() function in C

Introduction: The floor() used as a function in the C programming language. This function uses to return the largest or most significant integer value. The integer value is smaller than...

3 minutes read.

Assert() Function in C

Assert (): In C, the statements are executed with the exit statement. In C language declare, and it tests the condition parameters. If the statement executed will be false, it...

3 minutes read.

Fibonacci Series in C Using For Loop

In this C article, we will let you know about the procedure of displaying the Fibonacci series of the first “n” positive integers. The syntax of for loop used in the...

4 minutes read.

abs() function in C

abs() function is a built-in function present in the stdlib.h header file. It returns an integer value. abs() function converts a negative value into a positive value. For example, if...

4 minutes read.

Execution flow of C program

There are various steps of the execution of the C program that is given below. The following step of the execution Write source codes Preprocess Compile Link edit Load Execute Editor or...

1 minute read.

Displaying Array in C

Reference a collection of variables of similar data type in an array using a single element. The parts are stored next to each other. When declaring an array, you must specify...

4 minutes read.

Conditional Operator in C

In C programming language, the conditional operator (also known as the ternary operator) is a shorthand way of writing an if-else statement. The syntax of conditional operator in C is...

3 minutes read.

Commenting in C

Commenting in C Commenting in the C language is used to give out the information about the lines of the code which are included. It is one of the things that...

3 minutes read.

Run Time Polymorphism in C

What is polymorphism? Polymorphism refers to the existence of various forms. Polymorphism can be simply defined as a message's capacity to be presented in multiple forms. An individual who can have...

4 minutes read.

Function in C

Function is a group of statements that are used to perform any task. In other words, we can say that a function is a self- contained a block of programs...

3 minutes read.

Memory leak in C

What is memory leak in C? Memory leak occurs when we keep allocating memory in the heap without freeing it, i.e., the allocated memory in heap is not released back to...

3 minutes read.

Find Day from Day in C Without using function

Introduction: In the given article, I find daily in C without using functions. It takes 365 days for the earth to revolve around the sun. It will be close to...

3 minutes read.

Socket Programming in C

Socket programming is a process that connects the two or more nodes on a networking communication with respective to each other. One of the sockets that determines the socket (I.e.,...

5 minutes read.