×

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

C pre-processor

The C processor is a macro processor which is used to compile the source code of the program (step by step) . It is not a part of the compiler....

4 minutes read.

memcpy() in C

Introduction: Here we discuss about the memcpy() function in C. The memcpy() function is also called the Copy Memory Block function. Used to create a copy of a specific drawing...

4 minutes read.

Heap Sort in C

In this tutorial, we will learn about heap sorting in C language, but before going to Heap sort, we have to know the concept of Complete Binary Tree. What is Complete...

8 minutes read.

C Programming Tutorial

What is C Programming C is most popular and widely used computer programming language. It was developed by Dennis Ritchie in 1972 at the Bell Lab. It is used to develop system application software. There...

8 minutes read.

Union in C

Union is a user-defined data type that is used to hold the different types of elements like structure. In union, all members share the same memory location. Syntax: union [union name] { ...

1 minute read.

Modulus on Negative Numbers in C

In this tutorial, we will explore some examples of modulus on negative number. What is Modulus of Negative number? By omitting the minus sign, one can determine the modulus of a negative...

3 minutes read.

Error handling in C

Error handling in C: The C programming standard does not provide direct convenience for handling the errors. However, being a system programming language, it definitely will give access to handling...

4 minutes read.

Difference between Array and List in C

C# Array vs List is where the abstraction and implementation of human computing meet.  Arrays are incredibly related to the hardware concept of contiguous and contiguous memory, where each part is...

3 minutes read.

Binary Search in C with Best and Worst Time Complexity

What is Binary Search? Binary search is an algorithm that is used to find an element in a sorted array efficiently. It has a time complexity of O(log n). It means...

3 minutes read.

C program that does not Suspend when Ctrl+Z is Pressed

In Programming when a program breakdown and runs surprisingly in the terminal compiler the developer has the ability to prevent the program from running unequivocally. For expressly halting the program,...

3 minutes read.

C program to find factorial of a number using Recursion

What does the term "Factorial of a Number" mean? In mathematics, factorial is the product of all positive integers that are less than or equal to a certain positive integer, and...

2 minutes read.

Decimal to Binary in C

What is a decimal number? A decimal number is a number represented in the decimal number system. This system of binary conversion uses base 10 to represent numbers, i.e. the digits...

3 minutes read.

Merge Sort in C

The merge sort follows the principle of the divide and conquers algorithm. Firstly it divides the input of an array into two halves and then calls itself for the two...

4 minutes read.

Functions in C

What is function? Functions are defined as the set of announcements which take inputs, perform operations and provide results. The operation of a function only performs when the function gets called....

4 minutes read.

memcmp() in C

Introduction: In this article we are discuss about memcmp() function in C. This function permits the person to evaluate the bytes of the two characters, as mentioned above. Depending on...

4 minutes read.

What is the main in C?

In the C programming language, "main" is a predefined term or function. It is the primary function in every C program and is in charge of beginning and terminating the...

6 minutes read.

Expressions in C

Expressions in C: In the C programming language, an expression defines a formula in which the operands are linked to each other by using operators to compute the value. The operand...

4 minutes read.

strtok() and strtok_r() functions in C with examples

strtok() and strtok_r() functions in C with examples C offer various strtok() and strtok r() methods for a string to be separated by a certain delimiter. Dividing a string is a...

2 minutes read.

Classification of Programming language

 Classification of Programming language  The programming language represents a set of instructions compiled along with each other to perform a specific task provided by the CPU (Central Processing Unit). A programming...

3 minutes read.

While-Loop in C

Syntax of While Loop The syntax that has been used for the while loop in C programming language is: while (termination condition) {   // the body of the loop  } Working of While-Loop Initially, we...

3 minutes read.