Declaring a Struct
Part of the Logic & Flow section of Coddy's C journey — lesson 39 of 63.
Now that you understand what a struct is, it's time to learn how to create your own structure types. Declaring a struct in C follows a specific syntax that defines the blueprint for your custom data type.
The basic syntax for declaring a struct is:
struct StructureName {
datatype member1;
datatype member2;
// ... more members
};Here's how each part works: you start with the struct keyword, followed by a name for your structure, then list all the member variables inside curly braces. Each member is declared just like a regular variable with its data type and name. Don't forget the semicolon after the closing brace!
For example, to create a structure that represents a point in 2D space:
struct Point {
int x;
int y;
};This declaration creates a new data type called struct Point that contains two integer members: x and y. Once declared, this structure type can be used throughout your program to create variables that hold coordinate information in an organized way.
Challenge
EasyCreate a C program that defines a struct to represent a book in a library system. Your program should:
- Define a
structnamedBookthat contains the following members:- An integer
idto store the book's identification number - A character array
titlewith size 50 to store the book's title - An integer
pagesto store the number of pages - A float
priceto store the book's price
- An integer
- After defining the struct, print a confirmation message:
Book struct defined successfully! - Print the size of the
Bookstruct in bytes usingsizeof()in the format:Size of Book struct: [size] bytes - Print the size of each member individually:
Size of id: [size] bytesSize of title: [size] bytesSize of pages: [size] bytesSize of price: [size] bytes
This challenge tests your understanding of struct declaration syntax, including the proper use of the struct keyword, member declarations with appropriate data types, and the semicolon after the closing brace. You'll also explore how C allocates memory for struct members using the sizeof() operator.
Remember that the exact size values may vary depending on your system's architecture, but the struct definition syntax must be correct for the program to compile and run successfully.
Cheat sheet
To declare a struct in C, use the following syntax:
struct StructureName {
datatype member1;
datatype member2;
// ... more members
};Start with the struct keyword, followed by a name for your structure, then list all member variables inside curly braces. Each member is declared with its data type and name. Don't forget the semicolon after the closing brace!
Example of a Point structure:
struct Point {
int x;
int y;
};This creates a new data type called struct Point with two integer members that can be used throughout your program.
Try it yourself
#include <stdio.h>
int main() {
// TODO: Define your Book struct here
// Print confirmation message
printf("Book struct defined successfully!\n");
// TODO: Print the size of the Book struct and its members
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