Structures is used to store related information in a structured way, making it easier to organize and manipulate data. A structure pointer in c is a special type of pointer that points to a structure variable. This article discussed the basics of structure pointers in c, including how to declare and use them.
What is Structure in C?
In C, a structure is a user-defined data type that allows you to group together variables of different data types under a single name. This makes it easier to manage related data and manipulate it in a structured way. A structure is defined using the "struct" keyword, followed by the name of the structure and a list of variables that make up the structure.
To learn more about Structure in C, you can read this article.
How To Declare Structure Pointer in C?
A structure pointer in C is declared using the same syntax as a regular pointer variable, with the addition of the structure type. The general syntax for declaring a structure pointer in C is as follows:
struct *;
Here, < structure_name> refers to the name of the structure type, and < pointer_name> is the name of the pointer variable.
Before we can use a structure pointer in c, it must be initialized to a variable. To initialize a variable, use the & operator to get the address of the structural variable.
structure_pointer = &structure_variable;
The structure pointer in c can also be initialized during the declaration.
Accessing Structure Member Using Pointer
Members of a structure can be accessed using a structure pointer in c. Below we have discussed some examples which show how to access members of a structure using a structure pointer in C:
Example 1: Accessing the structure members using the dot operator
This example demonstrates how to access members of a structure using the dot operator. Below is the code implementation and explanation.
#include <stdio.h> #include <string.h> struct PrepBuddy { char name[25]; int age; }; int main() { struct PrepBuddy person; struct PrepBuddy* personPtr = &person; strcpy((*personPtr).name, "PrepBytes"); (*personPtr).age = 20; printf("Name: %s\n", (*personPtr).name); printf("Age: %d\n", (*personPtr).age); return 0; }
Output
Name: PrepBytes
Age: 20
Explanation: In this example, the "name" and "age" members of the "PrepBuddy" structure are accessed using the dot operator. The "strcpy()" function is used to copy the string "PrepBytes" into the "name" member of the "PrepBuddy" structure, and the integer value 20 is assigned to the "age" member. The "printf()" function is used to print the values of the "name" and "age" members to the console.
Example 2: Accessing the structure members using the arrow operator.
This example demonstrates how to access members of a structure using the arrow operator. Below is the code implementation and explanation.
#include <stdio.h> #include <string.h> struct PrepBuddy { char name[25]; int age; }; int main() { struct PrepBuddy person; struct PrepBuddy* personPtr = &person; strcpy(person.name, "PrepBytes"); person.age = 20; printf("Name: %s\n", personPtr->name); printf("Age: %d\n", personPtr->age); strcpy(personPtr->name, "Raghav"); personPtr->age = 25; printf("Name: %s\n", person.name); printf("Age: %d\n", person.age); return 0; }
Output
Name: PrepBytes
Age: 20
Name: Raghav
Age: 25
Explanation: In this example, a structure named "PrepBuddy" is defined with two members – "name" of type char and "age" of type int. A variable of this structure type named "person" is declared and initialized with some values. A pointer to this structure type named "personPtr" is also declared and initialized with the address of "person". Using the -> operator, the "name" and "age" members of the structure pointed to by "personPtr" are accessed and printed. Then, the values of the "name" and "age" members are changed using the pointer variable, and the new values are printed.
Example 3: Accessing a structure member within a structure using an arrow operator
This example demonstrates how to access members of a structure within another structure using the arrow operator. Below is the code implementation and explanation.
#include <stdio.h> #include <string.h> struct Date { int day; int month; int year; }; struct Person { char name[25]; int age; struct Date birthdate; }; int main() { struct Person prepBuddy; struct Person *personPtr = &prepBuddy; strcpy(prepBuddy.name, "Himanshu"); prepBuddy.age = 25; prepBuddy.birthdate.day = 1; prepBuddy.birthdate.month = 1; prepBuddy.birthdate.year = 1998; printf("Name: %s\n", personPtr->name); printf("Age: %d\n", personPtr->age); printf("Birthdate: %d/%d/%d\n", personPtr->birthdate.day, personPtr->birthdate.month, personPtr->birthdate.year); return 0; }
Output
Name: Himanshu
Age: 25
Birthdate: 1/1/1998
Explanation: In this example, the "birthdate" member of the "Person" structure is a nested structure. The -> operator is used to access the "birthdate" member within the "person" structure pointed to by "personPtr".
Summary
Here is a summary of key points which we discussed in structure pointer in c.
- Structure pointer in c is a pointer that points to a structure variable.
- They are declared using the structure name followed by an asterisk (*) and a variable name.
- The -> operator is used to access the members of the structure pointed to by the structure pointer.
- Accessing a structure member using a pointer involves dereferencing the pointer using the * operator and then using the . operator to access the member.
- Nested structures can be accessed using the -> operator in a similar manner.
FAQs
Here are some FAQs on Structure Pointers in C.
Q1: How is a structure pointer assigned to another structure pointer?
Ans: A structure pointer can be assigned to another structure pointer using the assignment operator (=).
Q2: Can a structure pointer be assigned to a void pointer?
Ans: Yes, a structure pointer can be assigned to a void pointer.
Q3: What is the purpose of typecasting a void pointer to a structure pointer in c?
Ans: Typecasting a void pointer to a structure pointer allows the structure members to be accessed and manipulated.
Q4: How is dynamic memory allocation for a structure pointer in c is performed?
Ans: Dynamic memory allocation for a structure pointer is performed using the malloc() function.
Q5: How is dynamic memory deallocation for a structure pointer in c is performed?
Ans: Dynamic memory deallocation for a structure pointer is performed using the free() function.
Q6: What is the difference between a structure and a structure pointer in C?
Ans: A structure in c is a variable that contains multiple members, while a structure pointer in c is a pointer that points to a structure variable.
Q7: Can a structure pointer in c be used with arrays?
Ans: Yes, a structure pointer in c can be used with arrays to access and manipulate the members of an array of structures.