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 Search in Array of Struct in C? In C, a struct (short for structure) is a user-defined data type that allows us to combine data items of different kinds. An array of structs is an array in which each element is of struct type. In this article, we will learn how to search for a specific element in an array of structs. Example: Inpu 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 Sort an Array of Structs with qsort in C? Sorting C arrays of structs becomes important for many kinds of applications and one common library function that may be used for this is qsort. In this article, we will learn how to use qsort() with an array of structs in C. For Example, Input: struct Person people[] = {{"Person1", 21}, {"Person2", 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 Array of Pointers to Strings in C In C, arrays are data structures that store data in contiguous memory locations. Pointers are variables that store the address of data variables. We can use an array of pointers to store the addresses of multiple elements. In this article, we will learn how to create an array of pointers to strings 2 min read Like