Learn C - Structures Cheatsheet - Codecademy
Learn C - Structures Cheatsheet - Codecademy
Structures
Structures are defined with the struct keyword // `struct` keyword and structure name
followed by the structure name. Inside the braces,
struct Person{
member variables are declared but not initialized. The
given code block defines a structure named Person // uninitialized member variables
with declared member variables name and age . char* name;
int age;
};
Structure data types are initialized using the struct // `Person` structure declaration
keyword with the defined structure type followed by
struct Person{
the name of the variable. The given code block shows
two ways to initialize Person type structures named
char* name;
person1 and person2 . int age;
};
https://www.codecademy.com/learn/learn-c-functions-and-structures/modules/structures-c-next-steps/cheatsheet 1/5
16/05/2024, 21:50 Learn C: Functions and Structures: Structures Cheatsheet | Codecademy
Structures can group different data types together into // `Person` structure definition
a single, user-defined type. This differs from arrays
struct Person{
which can only group the same data type together into
a single type. The given code block defines a structure // member variables that vary in type
named Person with different basic data types as char* name;
member variables.
int age;
char middleInitial;
};
// initialization of `person1`
struct Person person1 = {.name = "George",
.age = 28, .middleInitial = "C"};
https://www.codecademy.com/learn/learn-c-functions-and-structures/modules/structures-c-next-steps/cheatsheet 2/5
16/05/2024, 21:50 Learn C: Functions and Structures: Structures Cheatsheet | Codecademy
The variables defined within a structure are known as // Person structure declaration
member variables. The given code block defined a
struct Person{
structure named Person with member variables
name of type char* , and age of type int . // member variables
char* name;
int age;
};
Pointers to a structure can be defined using the struct // Person structure declaration
keyword, the structure type, and the pointer ( * ) struct Person{
symbol. The memory address of an initialized structure
// member variables
can be accessed using the symbol ( & ). The given code
block defines a pointer to a Person data type named char* name;
person1 . int age;
};
// person1 initialization
struct Person person1 = {"George", 28};
https://www.codecademy.com/learn/learn-c-functions-and-structures/modules/structures-c-next-steps/cheatsheet 3/5
16/05/2024, 21:50 Learn C: Functions and Structures: Structures Cheatsheet | Codecademy
// `person1` intialization
struct Person person1 = {"Jerry", 29};
https://www.codecademy.com/learn/learn-c-functions-and-structures/modules/structures-c-next-steps/cheatsheet 4/5
16/05/2024, 21:50 Learn C: Functions and Structures: Structures Cheatsheet | Codecademy
Print Share
https://www.codecademy.com/learn/learn-c-functions-and-structures/modules/structures-c-next-steps/cheatsheet 5/5