Creating Struct Variables
Part of the Logic & Flow section of Coddy's C journey — lesson 40 of 63.
Once you've declared a struct type, you can use it to create actual variables that hold data. Creating a struct variable works exactly like declaring any other variable in C - you specify the data type followed by the variable name.
The syntax for creating a struct variable is:
struct StructureName variableName;For example, using the Point struct from the previous lesson:
struct Point p1;
struct Point p2;This creates two variables, p1 and p2, both of type struct Point. Each variable has its own separate copy of the x and y members defined in the struct.
You can create as many struct variables as you need, just like you would with built-in types like int or float. Each struct variable is independent and can hold different values in its members. Once created, these variables are ready to store coordinate data in an organized way.
Challenge
EasyCreate a C program that demonstrates creating multiple struct variables of different types. Your program should:
- Define a
structnamedRectanglewith the following members:- An integer
width - An integer
height
- An integer
- Define a
structnamedStudentwith the following members:- An integer
id - A character array
namewith size 30 - A float
gpa
- An integer
- In the main function, create the following struct variables:
- Two
Rectanglevariables namedrect1andrect2 - Three
Studentvariables namedstudent1,student2, andstudent3
- Two
- Print confirmation messages for each variable created in this exact format:
Rectangle variable rect1 createdRectangle variable rect2 createdStudent variable student1 createdStudent variable student2 createdStudent variable student3 created
- Print the total number of struct variables created in the format:
Total struct variables created: 5 - Print the memory usage for each type in the format:
Memory per Rectangle: [size] bytesMemory per Student: [size] bytes
This challenge tests your ability to define multiple struct types and create several variables of each type. Each struct variable is independent and has its own memory space, demonstrating how structs can be used to organize different types of data in your programs.
Cheat sheet
To create struct variables, use the same syntax as declaring other variables:
struct StructureName variableName;Example creating multiple struct variables:
struct Point p1;
struct Point p2;Each struct variable is independent and has its own separate copy of all the members defined in the struct. You can create as many struct variables as needed, just like built-in types.
Try it yourself
#include <stdio.h>
int main() {
// TODO: Define your struct Rectangle here
// TODO: Define your struct Student here
// TODO: Create the struct variables here
// TODO: Print confirmation messages for each variable created
// TODO: Print total number of struct variables created
// TODO: Print memory usage for each struct type using sizeof()
return 0;
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Logic & Flow
1Pointers Fundamentals
What is a Pointer?Declaring PointersThe Address-Of Operator (&)The Dereference Operator (*)NULL PointersRecap: Pointer Basics4Project: Simple Text Utility
Project OverviewCounting Characters7Structures (structs)
What is a Struct?Declaring a StructCreating Struct VariablesAccessing Struct MembersInitializing StructsRecap: Student Data Struct2Pointers and Arrays
Array Names as PointersArray Elements - PointersPointer ArithmeticComparing PointersRecap: Pointer Array Traversal5Pointers and Functions
Pass-by-ValuePassing Pointers to FunctionsModifying Vars via PointersA Classic Example: SwapPassing Arrays to FunctionsRecap: Function Pointer Args8Structs and Pointers
Pointers to StructsThe Arrow Operator (->)Passing Structs by ValuePassing Struct PointersDynamic Allocation of StructsRecap: Modifying Struct - Ptr11Final Recap Challenges
Recap: Dynamic String ConcatRecap: Array of StructsRecap: Word Frequency Counter3Character Arrays and Strings
Strings as char ArraysThe Null TerminatorString Input with scanfUsing strlen()Using strcpy()Using strcat()Using strcmp()Recap: Basic String Functions