Menu
Coddy logo textTech

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 icon

Challenge

Easy

Create a C program that demonstrates creating multiple struct variables of different types. Your program should:

  1. Define a struct named Rectangle with the following members:
    • An integer width
    • An integer height
  2. Define a struct named Student with the following members:
    • An integer id
    • A character array name with size 30
    • A float gpa
  3. In the main function, create the following struct variables:
    • Two Rectangle variables named rect1 and rect2
    • Three Student variables named student1, student2, and student3
  4. Print confirmation messages for each variable created in this exact format:
    • Rectangle variable rect1 created
    • Rectangle variable rect2 created
    • Student variable student1 created
    • Student variable student2 created
    • Student variable student3 created
  5. Print the total number of struct variables created in the format: Total struct variables created: 5
  6. Print the memory usage for each type in the format:
    • Memory per Rectangle: [size] bytes
    • Memory 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;
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Logic & Flow