Access Structure Members: The Structure members can be accessed using two ways.
1. Access Structure member using dot(.) Opertor.
2. Access Structure member through pointer -> Operator.
Access structure members in C programming.
1. Access Structure member using dot(.) Opertor.
The dot(.) operators is used to access the members of a structure variable. The dot (.) operator is also called membership operator. The dot(.) operator is used to specify the name of the structure member we want to access. Here's is the example in C programming how we can access the members of the struct employee:
#include <stdio.h>
#include <string.h>
struct employee {
char ename[50];
int empno;
float salary;
};
int main() {
struct employee e1;
// Accessing structure members
strcpy(e1.ename, "Ajay Dev");
e1.empno = 25;
e1.salary=18036.1;
// Printing structure member values
printf("Name: %s\n", e1.ename);
printf("Emp No: %d\n", e1.empno);
printf("Salary: %.2f\n", e1.salary);
return 0;
}
Output:
Name: Aja Dev
Emp No: 25
Salary:18036.10
In C language, the pointers is used to access structure structure members. The (->) operator is used to access structure members , we use the arrow (->) operator. The operator -> is used when the programmer have a pointer to a structure variable. An Arrow operator in C Language allows to access elements in Structure. The -> operator is used with a pointer variable pointing to a structure . The operator is formed by using a minus sign(-), followed by the greater than (>) symbol as shown below.
Syntax:
(name_of_pointer)->(name_of_variable)
Operation: The -> operator in C Language gives the value held by name_of_variable to structure or union variable name_of_pointer.
Difference between Dot(.) and Arrow(->) operator:
The Dot(.): operator is used to access members of a structure.
The Arrow(->) : the structure members can be accessed by -> operator using pointers.
2. Access Structure member through -> pointer operator.
#include <stdio.h>
#include <string.h>
struct employee {
char ename[25];
int age;
float salary;
};
int main() {
struct employee person1;
// Declare a pointer to a structure
struct employee *personPtr;
// Assign the address of person1 to the pointer
personPtr = &person1;
// Accessing structure members through pointer
strcpy(personPtr->ename, "Raj M.");
personPtr->age = 25;
personPtr->salary = 34506.1;
// Printing structure member values
printf("Name: %s\n", personPtr->ename);
printf("Age: %d\n", personPtr->age);
printf("Salary: %.2f\n", personPtr->salary);
return 0;
}
Output:
Name: Raj M.
Age: 25
Salary: 34506.10
Previous Topic:-->> Declare Structure Variable || Next topic:-->>Structure Initialization
Other Topics:
Variables and Identifiers
Relational Operators
if-else statements
Switch case
While Loop
Infinite while Loops
C FOR Loop
Infinite for Loops
Continue in Loops
One Dimensional Array
Two Dimensional Arrays
Read and Display 2D Arrays
Types of functions
Passing Array To Functions
Nesting of Function
Array vs Structure
Array of Structure
Structures and Functions
Structures Within Structures
Use Of Pointers In C
File Handling In C
Loops FAQ
Arrays FAQ
count vowels in a file
Function FAQ
Conditional Statements Assignments
For Loops Assignments
Arrays Assignments
Function Assignments
Structure Assignments
Pointers Assignments
Files Assignments
Storage classes Assignments
Binary Files
count words,lines in a file
Copy files
Update File
Continue in Loops
break in Loops
Difference Between While and Do while
difference while do..while & for
malloc
calloc
Storage Classes
Operators MCQ
Conditional Statements MCQ
Loops MCQ
Arrays MCQ
Function MCQ
Structure MCQ
Pointers MCQ
Files MCQ
Storage classes MCQ