Accessing structure members

Accessing structure members

If you want to access the structure members individually in the C program, then you can use the dot (.) operator in the structure to work directly with the declared structure variable. Similarly, when you use pointers in the structure, you call the structure members using the arrow (->) operator.

Accessing structure members

So let’s access the structure member in the C program with dot operator and pointer operator.

Accessing structure members using dot (.) operator in the structure.

Similarly, if you want to use the pointer operator with the declared variable in the C structure program, then you have to use the dot (.) operator to access the declared structure member.

#include <stdio.h>

#include <string.h>

// Define a employee structure data type

struct employee {

char name[60];

int age;

float salary;

};

int main() {

// Declare individual employee structure variable

struct employee employee0;

// now individually assign values ​​​​to structure members

strcpy(employee0.name, “Amit Jain”);

employee0.age = 55;

employee0.salary = 10000.56 ;

// Now Access and print data of individual structure members

printf(“\n Employee Name – %s”, employee0.name);

printf(“\n Employee Age – %d”, employee0.age);

printf(“\n Employee salary – %f “, employee0.salary);

return 0;

}

In this employee structure program example.

employee0.name, employee0.age, and employee0.salary are structure individual variable data types accessed using dot (.) operator.

Calling structure member using arrow (->) operator in C structure.

If you want to call a structure member with a pointer in a structure program. Then you have to use the arrow (->) operator to access the declared structure member.

#include <stdio.h> // standard input/output header file in c

#include <stdlib.h> // use for malloc function

#include <string.h> // use for string strcpy function

// Define a employee structure data type

struct employee {

char name[60];

int age;

float salary;

};

int main() {

// lets declare a pointer to a structure data type

struct employee *ptremployee;

// let acclocate memory for pointer variable

ptremployee = malloc(sizeof(struct employee));

// Check if the memory allocation is successful or not for employee structure

if (ptremployee == NULL) {

fprintf(stderr, “\n employee structure variable memory allotment unsucessfull.”);

return 1;

}

// let Assign values ​​​​to the employee structure members with pointer operator

strcpy(ptremployee->name, “kunal shah”);

ptremployee->age = 55;

ptremployee->salary = 9999. 60;

// let Access and print the employee structure members with pointer operator

printf(“\n Employee name – %s”, ptremployee->name);

printf(“\n Employee age – %d”, ptremployee->age);

printf(“\n Employee salary – %f”, ptremployee->salary);

// let Free the alloted memory when structure member execute

free(ptremployee);

return 0;

}

Example of the pointer to structure operator above.

ptremployee->name, ptremployee->age, and ptremployee->salary can be called using the pointer arrow (->) operator.

In Employee structure, malloc(sizeof(struct employee)) function is used to allocate dynamic memory for ptremployee.

When the memory is not required for the data type variable in Employee structure, then the memory can be free() using free(ptremployee) function.

Access Structure Member with Dot Pointer Operator in C.

Whenever you want to access the structure variable, then to access the individual structure member, use the dot (.) operator.

To call structure data types with pointers or to access structure members, use the arrow (->) pointer to structure operator.