How to Pass Array of Structure to a Function in C? (original) (raw)
Last Updated : 19 Mar, 2024
An array of structures in C is a data structure that allows us to store multiple records of different data types in a contiguous memory location where each element of the array is a structure. In this article, we will learn how to pass an array of structures from one function to another in C.
Passing an Array of Struct to Functions in C
We can pass an array of structures to a function in a similar way as we pass an array of any other data type i.e. by passing the array as the pointer to its first element.
Syntax to Pass Array of Struct to Function in C
// using array notation returnType functionName(struct structName arrayName[], dataType arraySize);
// using pointer notation returnType functionName(struct structName *arrayName, dataType arraySize) );
Here,
- functionName is the name of the function.
- structName is the name of the struct.
- *arrayName is a pointer to the array of structures.
- arraySize is the size of an array of structures.
C Program to Pass Array of Struct to Function
The below program demonstrates how we can pass an array of structures to a function in C.
C `
// C Program to pass array of structures to a function #include <stdio.h>
// Defining the struct struct MyStruct { int id; char name[20]; };
// Function to print the array of structures void printStructs(struct MyStruct* array, int size) { for (int i = 0; i < size; i++) { printf("Struct at index %d: ID = %d, Name = %s\n", i, array[i].id, array[i].name); } }
int main() { // Declaring an array of structs struct MyStruct myArray[] = { { 1, "P1" }, { 2, "P2" }, };
// Passing the array of structures to the function
printStructs(myArray, 2);
return 0;
}
`
Output
Struct at index 0: ID = 1, Name = P1 Struct at index 1: ID = 2, Name = P2
Similar Reads
- How to Pass a 3D Array to a Function in C? A 3D array (or three-dimensional array) in C is a multi-dimensional array that contains multiple layers of two-dimensional arrays stacked on top of each other. It stores elements that can be accessed using three indices: the depth index, row index, and column index. In this article, we will learn ho 3 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 Access Array of Structure in C? In C, we can create an array whose elements are of struct type. In this article, we will learn how to access an array of structures in C. For Example, Input:myArrayOfStructs = {{'a', 10}, {'b', 20}, {'A', 9}}Output:Integer Member at index 1: 20Accessing Array of Structure Members in CWe can access t 2 min read
- How to Find the Size of an Array in C? The size of an array is generally considered to be the number of elements in the array (not the size of memory occupied in bytes). In this article, we will learn how to find the size of an array in C.The simplest method to find the size of an array in C is by using sizeof operator. First determine t 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 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 Use bsearch with an Array of Struct in C? The bsearch function in C is a standard library function defined in the stdlib.h header file that is used to perform binary search on array-like structure. In this article, we will learn to use bsearch with an array of struct in C. Input: struct Person people[] = { { 1, "Ram" }, { 2, "Rohan" }, { 4, 3 min read
- How to Create a Dynamic Array Inside a Structure? In C, the structure can store the array data types as one of its members. In this article, we will learn how to create a dynamic array inside a structure in C. Creating a Dynamic Array Inside a Structure in CTo create a dynamic array inside a structure in C, define a structure that contains a pointe 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 Dynamically Create Array of Structs in C? In C, an array is a data structure that stores the collection of elements of similar types. Structs in C allow the users to create user-defined data types that can contain different types of data items. In this article, we will learn how we can create an array of structs dynamically in C. Dynamicall 2 min read