How to Modify Struct Members Using a Pointer in C? Last Updated : 08 Feb, 2024 Comments Improve Suggest changes Like Article Like Report 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'; Modify Struct Members Using PointerWe 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;C++ Program to Modify Struct Members Using a Pointer C // C program to modify structure members using pointer #include <stdio.h> typedef struct myStructure { int mem1; char mem2 } MyStructure; int main() { MyStructure myStruct = { 10, 'a' }; MyStructure* myStructPtr = &myStruct; printf("Original Structure: (%d, %c)\n", myStruct.mem1, myStruct.mem2); myStructPtr->mem1 = 28; (*myStructPtr).mem2 = 'z'; printf("Modified Structure: (%d, %c)\n", myStruct.mem1, myStruct.mem2); return 0; } OutputOriginal Structure: (10, a) Modified Structure: (28, z) Time Complexity: O(1)Space Complexity: O(1) Comment More infoAdvertise with us Next Article How to Modify Struct Members Using a Pointer in C? C coderwrioiiy Follow Improve Article Tags : C Programs C Language C-Struct-Union-Enum C-Pointers C Examples +1 More Similar Reads How to Sort an Array of Structs Based on a Member in C? In C, we may sometimes need to sort the array of structs based on the values of certain members. In this article, we will learn how to sort a given array of structures based on a specific member in C. For Example, Input: myArrayofStructs[] = {{"Person1", 21, 160.5}, {"Person2", 20, 175.0}, {"Person3 2 min read How to Declare a Pointer to a Struct in C? Structure (or structs) in the C programming language provides a way to combine variables of several data types under one name and pointers provide a means of storing memory addresses. In this article, we will learn how to declare such a pointer to a struct in C. Declaration of Pointer to Struct in C 2 min read How to Declare a Struct Member Inside a Union in C? A union contains different types of variables as its members and all these members share a memory location. In this article, we will learn how to declare and access the struct member inside a union in C++. Structure Inside Union in CTo declare a structure inside a union we can use the below syntax: 2 min read How to Initialize Array of Pointers in C? Arrays are collections of similar data elements that are stored in contiguous memory locations. On the other hand, pointers are variables that store the memory address of another variable. In this article, we will learn how to initialize an array of pointers in C. Initialize Array of Pointers in CWe 2 min read How to Declare a Pointer to a Union in C? Union is a user-defined data type in C language that can contain elements of the different data types and pointers are used to store memory addresses. In this article, we will learn how to declare a pointer to a union in C++. Pointer to Union in CTo declare a pointer to a union first, define a union 2 min read How to Iterate Through Array of Structs in C? In C, while working with an array of structs we have to iterate through each struct in an array to perform certain operations or access its members. In this article, we will learn how to iterate through an array of the structs in C. Iterate Over of an Array of StructuresTo iterate through an array o 2 min read How to Initialize Char Array in Struct in C? In C++, we can also define a character array as a member of a structure for storing strings. In this article, we will discuss how to initialize a char array in a struct in C. Initialization of Char Array in Struct in CWhen we create a structure instance (variable) in C that includes a char array, we 2 min read How to Initialize Array of Structs in C? In C, arrays are data structures that store the data in contiguous memory locations. While structs are used to create user-defined data types. In this article, we will learn how to initialize an array of structs in C. Initializing Array of Structures in CWe can initialize the array of structures usi 2 min read How to avoid Structure Padding in C? Prerequisites: Structure Member Alignment, Padding and Data Packing In Structure, sometimes the size of the structure is more than the size of all structures members because of structure padding. Below is an example of Structure padding: C // C program to show an example // of Structure padding #inc 2 min read How to use typedef for a Struct in C? In C, we use typedef to create aliases for already existing types. For structure, we can define a new name that can be used in place of the original struct name. In this article, we will learn how to create a typedef for a structure in C++. Use the typedef struct in CTo create an alias for a structu 2 min read Like