×

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

Type Casting in C

Type casting is a way to convert a variable from one data type to another data type.Syntax: (type) expression;     Example: #include <stdio.h> int main() { int a,b; float sum; printf("Example of Type Casting\n"); printf("Enter any integer nos:"); scanf("%d",&a,&b); sum=a+b; printf("Sum: %f",sum); return 0; } Output Example...

1 minute read.

Ferror() in c

Ferror():  In C, the ferror() function checks assuming there is a blunder in the given stream. The ferror() function is utilized to check for the document blunder on given stream. A return...

4 minutes read.

Compilation Process in C language

What is the Compilation Process?  The compilation is a method whereby the source code is converted into object code. It is achieved with compiler assistance. The compiler tests the source code for syntactic...

3 minutes read.

C Switch Statements

Switch-case statement comes under the Selection control statement; there are four types of Control statements Decision-making statements (if, if-else)Selection statements (Switch-case)Iteration statements (for, while, do-while)Jump statements (break, continue, goto) If we want...

4 minutes read.

strcat() Function in C

Strings in c: A string is defined as the set of characters that are enclosed within the double quotations (" "). The String always ends with the null character ("\0"). The...

3 minutes read.

Length of an Array Function in C

In C, there is no built-in function to get the length of an array. However, there are a few ways you can determine the length of an array. It is necessary...

15 minutes read.

Errors in C

Errors in C Errors are nothing but problems or faults that pretty much occur in all the programming languages. Errors make the behavior of the program seem abnormal, and even the...

4 minutes read.

Find reverse of an array in C using functions

Introduction: Here, we describe how to find the reverse array in C using functions. Suppose you have an array with n elements. I need to display the elements present in...

3 minutes read.

Strcpy() in C

In C language many operations can be performed on a string. Characters in a sequence are known as string. Note:   To use this function we must include the #include<string.h> header file...

4 minutes read.

How to install a C compiler in windows 10-64bit

Many C compilers can be installed on your pc. The most downloaded compilers include GCC, Turbo C++, Visual Studio, etc. This article includes the installation of the Turbo C++ compiler in...

6 minutes read.

Consumer billing system in C

Introduction : Billing has always been taught to do perfectly, we know today's world is full of products, and We all are using multiple products. We are buying and selling various...

9 minutes read.

User Defined Functions in C

C Functions: A function is defined as a block of code that is used to perform some specific task. In functions, we use flower brackets ({}) which contain the set of programming...

5 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.

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.

Nested if-else statement in C

If we use an if-else statement within another if statement in a C program, it is called a nested if-else statement in C. It helps to check the condition inside...

3 minutes read.

Limitations of Inline Function in C

The compiler is unable to conduct inlining in two instances. It just accepts the inline definitions and produces space for the function in the same way it does for a...

3 minutes read.

Types of Function in C

A function is a piece of code that accomplish a certain operation for a specific task. There are two types of functions in C: System-defined function (Library Function)User-defined function 1. System defined Function...

8 minutes read.

C Program Swap Numbers in cyclic order Using Call by Reference

The three integers that the user entered in cyclical sequence are swapped in this tutorial using call by reference. C Program: #include <stdio.h> void cyclic_Swap ( int *X, int *Y, int *Z ); int...

2 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.

Prime Number Program in C using for Loop

In this article, we will know about the procedure of checking whether a natural number inputted by the user is a prime number or non-prime number. Definition of the prime number A...

3 minutes read.