Learn C - Functions and Structures - Structures Cheatsheet - Codecademy
Learn C - Functions and Structures - 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;
};
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"};
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
can be accessed using the symbol ( & ). The given code // member variables
block defines a pointer to a Person data type named char* name;
person1 . int age;
};
// person1 initialization
struct Person person1 = {"George", 28};
// `person1` intialization
struct Person person1 = {"Jerry", 29};
// `person1Pointer` intialization to
memory address to `person1`
struct Person* person1Pointer = &person1;
Print Share