![]() |
VOOZH | about |
In C++, we use structure to group multiple different types of variables inside a single type. These different variables are called the members of structures. In this article, we will discuss how to modify the struct member using a pointer in C.
Example
Input: myStruct.mem1 = 10; myStruct.mem2 = 'a'; Output: myStruct.mem1 = 28; myStruct.mem2 = 'z';
We can declare a pointer variable that points to the instance of a structure. To modify the value of this structure member using the pointer, we can dereference the structure pointer and use either the dot operator (.) or the arrow operator(->) as shown:
myStructPtr->mem1 = 28;
or
(*myStructPtr).mem1 = 28;Original Structure: (10, a) Modified Structure: (28, z)
Time Complexity: O(1)
Space Complexity: O(1)