How to Declare a Pointer to a Struct in C? Last Updated : 05 Feb, 2024 Comments Improve Suggest changes Like Article Like Report 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 CTo declare a pointer to a struct we can use the struct keyword. First, define a structure then declare a pointer to that structure using the below syntax and that pointer can be used to allocate and access memory for the structure. Syntax of Declaring Pointer to Structstruct StructName *ptr ;orstruct { // struct members} *ptr ;C Program to Declare Pointer to StructThe below example demonstrates declaration of pointer to a structure. C++ // C Program to show how to declare and use pointer to // structure #include <stdio.h> #include <stdlib.h> // Define a struct named Point with x and y coordinates struct Point { int x; int y; }; int main() { // Declare a pointer to a Point struct struct Point* ptr; // Allocate memory for a Point struct using malloc ptr = (struct Point*)malloc(sizeof(struct Point)); // Assign values to the x and y coordinates ptr->x = 10; ptr->y = 20; // Print the coordinates printf("Coordinates: (%d, %d)\n", ptr->x, ptr->y); // Free the allocated memory free(ptr); return 0; } OutputCoordinates: (10, 20) To know more, refer to the article - Structure Pointer in C Comment More infoAdvertise with us Next Article How to Declare a Pointer to a Struct in C? K kumardhanshiv Follow Improve Article Tags : C Programs C Language C-Struct-Union-Enum C-Pointers C Examples +1 More Similar Reads 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 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 Create an Array of Structs in C? In C, a structure is a user-defined data type that can be used to group items of different types into a single entity while an array is a collection of similar data elements. In this article, we will learn how to create an array of structs in C. Creating an Array of Structs in CTo create an array of 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 Add an Element to an Array of Structs in C? In C, a struct is a user-defined data type that allows the users to group related data in a single object. An array of structs allows to store multiple structs in contiguous memory locations. In this article, we will learn how to add an element to an array of structs in C. Example: Input: structArra 3 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 Write a Struct to a Binary File in C? C file handling allows users to store the data from a C program to a file in either the text or the binary format. In this article, we will learn how to write a struct to a binary file in C. Writing Structure into a Binary File in CTo write a struct to a binary file, we can use the fwrite() function 2 min read How to Pack a Struct in C? In C, when you declare a structure, the compiler allocates memory for its members, and the way it does this can involve adding padding between members for alignment purposes. struct packing refers to the arrangement of the members of a structure in memory so that there is no extra space left. In thi 1 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 Create a Linked List in C? Write a C program to create a linked list.A linked list is a sequential data structure that stores the data in non-contiguous memory locations unlike array. In a linked list, each element is referred to as a node. Each node in the linked list stores data and a pointer to the next node present in the 5 min read Like