Strings, Containers and Memory

Arrays of Structs

How to store structs in an array and how structs and arrays differ.

Struct vs. Array: What's the Difference?

A struct groups named members that can differ in type, while an array holds numbered elements that all share one type. See when to use each in C++.

3 min read

What Is an Array of Structures in C?

An array of structures in C stores several structs of one type side by side in memory. Here is how to declare, fill, and loop over one, with examples.

3 min read

Related topics

More in strings, containers and memory

String Methods The main std::string member functions and how C strings work. Finding in Strings How to use find() to locate characters and substrings in a string. String Contains How to check whether a string holds a substring, plus the C string.h library. Comparing Strings How to compare strings in C++ and C, including strcmp and the == operator. Concatenating Strings How to join strings together in C++ and what concatenation means. Reversing Strings How to reverse a string with std::reverse and why strrev often fails. Iterating Over Strings How to loop through the characters of a string and other basic string operations. Splitting Strings How to split a string on a delimiter in C++, compared with split() in C# and Python. stringstream How stringstream, getline and the stream classes parse and build strings. Reading String Input How to read text input in C++ and related functions like strstr. Switching on Strings Workarounds for using strings in a switch, plus switch basics in C. String to Int How to turn a string into an integer with stoi, atoi and similar tools. Int to String How to turn numbers into strings and convert C strings to std::string. Char to Int How to convert a char to an int, how big a char is and how to_string helps. Vector Initialization Ways to create and fill a std::vector, and how size and resize work. Dynamic Arrays and Linked Lists Using std::vector as an ArrayList and how linked lists differ from arrays. std::array How std::array gives fixed size arrays a safer interface. std::list How the std::list container works and what the std:: prefix means. List vs Array When to pick a list or an array, with comparisons from C# and Python. Dictionaries with std::map How std::map works as a dictionary and the main data structures in C. Hash Sets How std::unordered_set works and how hash sets compare with maps and lists. std::set How std::set keeps unique sorted values, with a note on sets in Python. Tuples How std::tuple groups values and how tuples compare with lists in Python. std::optional How std::optional represents a value that may be missing. Returning Arrays Ways to return an array from a C++ function and how char arrays and pointers differ. Passing Arrays How arrays are passed to functions and what actually gets passed. Random Numbers How to generate random numbers in C++ and the limits of rand and srand. NULL vs nullptr Why C++ added nullptr, how it differs from NULL and 0 and what null pointers are. Heap vs Stack How stack and heap memory differ and where your variables live. Segmentation Faults What causes a segmentation fault and what SIGSEGV means. Modifiable Lvalue Error What lvalues are and how to fix the expression must be a modifiable lvalue error. Valgrind and Memory Leaks How to find memory leaks with Valgrind and set up a program for GDB.