C Academy · Lesson

Defining and Using Structures

Learn how to group related variables into a structure and use struct in real-world applications.

Lesson 1 of 310 steps

Defining and Using Structures is a free C Academy lesson on CoddyKit. This is lesson 1 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C Academy learning path, and your progress syncs across the web and the CoddyKit app. The C Academy course includes 3 lessons in total.

Defining and Using Structures

Structures (struct) allow us to group related variables under a single name.

In this lesson, you will learn:

  • How to define and use structures.
  • How to access structure members.
  • How to pass structures to functions.
Defining and Using Structures — illustration 1

What is a Structure?

A structure is a user-defined data type that groups different variables.

Example structure definition:

struct Person { char name[50]; int age; float height; };

Here, a Person structure stores name, age, and height.

Declaring and Initializing a Structure

After defining a structure, we can declare and initialize it:

struct Person p1 = {"Alice", 25, 5.7};

We can also assign values later:

p1.age = 30;

Accessing Structure Members

Structure members are accessed using the dot (.) operator.

Example:

printf("Name: %s\n", p1.name);

This prints the name stored in the structure.

Example: Using Structures

This program demonstrates defining, initializing, and accessing a structure.

#include <stdio.h>

struct Person {
    char name[50];
    int age;
    float height;
};

int main() {
    struct Person p1 = {"Alice", 25, 5.7};
    printf("Name: %s\n", p1.name);
    printf("Age: %d\n", p1.age);
    printf("Height: %.1f\n", p1.height);
    return 0;
}

Pointers to Structures

We can use pointers to structures for efficient memory management.

Example:

struct Person *ptr = &p1;

Members are accessed using the arrow (->) operator:

printf("%s", ptr->name);

Example: Structure Pointers

This program demonstrates accessing structure members using pointers.

#include <stdio.h>

struct Person {
    char name[50];
    int age;
};

int main() {
    struct Person p1 = {"Bob", 30};
    struct Person *ptr = &p1;
    printf("Name: %s\n", ptr->name);
    printf("Age: %d\n", ptr->age);
    return 0;
}

Passing Structures to Functions

Structures can be passed to functions by value or reference.

Example (pass by reference):

void update(struct Person *p) { p->age += 1; }

Passing by reference avoids copying large structures.

Summary

In this lesson, you learned:

  • How to define and use structures.
  • How to access structure members.
  • How to use pointers with structures.

Next, we will explore unions and memory efficiency in C!

Defining and Using Structures — illustration 10
Free to start

Learn C with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
39
Lessons
144

Frequently Asked Questions

Is the “Defining and Using Structures” lesson free?

Yes — the full text of “Defining and Using Structures” is free to read here on the web. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C Academy course, upgrade to CoddyKit PRO. The C Academy course includes 3 lessons in total.

What will I learn in “Defining and Using Structures”?

Learn how to group related variables into a structure and use struct in real-world applications. You practise C Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C Academy?

No prior experience is required. C Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 3.

How long does the “Defining and Using Structures” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C Academy lesson?

Yes. Every C Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Defining and Using Structures
  2. Unions and Memory Efficiency
  3. Enumerations (enum)
← Back to C Academy