Structures
Learn about structures in C.
StartKey Concepts
Review core concepts you need to learn to master this subject
Defining Structures With struct
Initializing Structures With struct
Custom Data Types With Structures
Grouping Data Types With Structures
Accessing Member Variables With Dot Notation
Structure Member Variables
Structure Type Pointers
Accessing Member Variables With Arrow Notiation
Defining Structures With struct
Defining Structures With struct
// `struct` keyword and structure name
struct Person{
// uninitialized member variables
char* name;
int age;
};
Structures are defined with the struct
keyword followed by the structure name. Inside the braces, member variables are declared but not initialized. The given code block defines a structure named Person
with declared member variables name
and age
.
- 1Throughout this course, we have defined many different variable types like, int and char. These are known as the basic types and are built-in to C. We’ve also defined data types like arrays and p…
- 2To help us understand how to include structures (also called structs) in our code, let’s look at how to define them: ![Structure components, the keyword struct, the name Bottle and member variable…
- 3Now that we know how to define structures, let’s take a look at how we can use them in our code. struct Bottle { char* name; int maxCapacity; int currentCapacity; }; struct Bottle bottle1 = …
- 4Let’s now take a step back and discuss why we would want to use structures. Look at an example of a program that uses bottle data without structures. char bottleName1[] = “Medium Bottle”; int maxC…
- 5Now that we’ve leveraged that power of structures to package variables together we can discuss how we can access each member variable individually using dot notation. Dot notation is a C operato…
- 6Similar to other derived types, like arrays and strings, structures can use a lot of memory. Imagine a structure with multiple strings each able to hold hundreds of characters. One way to manage …
- 7Let’s wrap up by using structures with functions. We can specify structures and pointers to structures as parameters to functions: void myFunction(struct Bottle b, struct Bottle* bPointer) When p…
What you'll create
Portfolio projects that showcase your new skills
How you'll master it
Stress-test your knowledge with quizzes that help commit syntax to memory