Functions and Structures: Structures Cheatsheet
Functions and Structures: Structures Cheatsheet
Structures
Structures are de!ned with the struct // `struct` keyword and structure name
keyword followed by the structure name. Inside the
struct Person{
braces, member variables are declared but not
initialized. The given code block de!nes a structure // uninitialized member variables
named Person with declared member variables char* name;
name and age . int age;
};
Structure data types are initialized using the // `Person` structure declaration
struct keyword with the de!ned structure
struct Person{
type followed by the name of the variable. The
given code block shows two ways to initialize char* name;
Person type structures named person1 int age;
and person2 . };
Structures can group di#erent data types together // `Person` structure definition
into a single, user-de!ned type. This di#ers from
arrays which can only group the same data type
struct Person{
together into a single type. The given code block // member variables that vary in
de!nes a structure named Person with type
di#erent basic data types as member variables.
char* name;
int age;
char middleInitial;
};
Accessing Member Variables With Dot Notation
// initialization of `person1`
struct Person person1 = {.name =
"George", .age = 28, .middleInitial =
"C"};
The variables de!ned within a structure are known // Person structure declaration
as member variables. The given code block de!ned
struct Person{
a structure named Person with member
variables name of type char* , and age of // member variables
type int . char* name;
int age;
};
Structure Type Pointers
// 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