access structure members in c programming Language.

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