Cheatsheets / Learn C: Functions and Structures
Structures
De!ning Structures With struct
struct
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;
};
Initializing Structures With struct
struct
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 . };
// designated initialization with
member variable names
struct Person person1 = {.name =
"Cosmo", .age = 36};
// implicit initialization following
order of member variables
struct Person person2 = {"George",
29};
Custom Data Types With Structures
Structures allow the de!nition of custom data
types that are used to represent complex data.
Structure customization provides the "exibility to
accurately model real-world data, giving you the
ability to access and modify the data from a single
de!ned variable.
Grouping Data Types With Structures
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
Initialized structure member variables can be // `Person` structure declaration
accessed with the dot ( . ) operator. The given
struct Person{
code block initializes a Person type named
person1 and accesses the name member // member variables
variable within a printf() statement. char* name;
int age;
char middleInitial;
};
// initialization of `person1`
struct Person person1 = {.name =
"George", .age = 28, .middleInitial =
"C"};
// accessing `name` in `person1`
printf("My name is %s", person1.name);
// OUTPUT: My name is George
Structure Member Variables
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
Pointers to a structure can be de!ned using the // Person structure declaration
struct keyword, the structure type, and the
struct Person{
pointer ( * ) symbol. The memory address of an
initialized structure can be accessed using the // member variables
symbol ( & ). The given code block de!nes a char* name;
pointer to a Person data type named int age;
person1 .
};
// person1 initialization
struct Person person1 = {"George",
28};
// person1Pointer initializated to the
memory address of person1
struct Person* person1Pointer =
&person1;
Accessing Member Variables With Arrow Notiation
Member variables of a structure can be accessed // `Person` structure declaration
using a pointer with arrow ( -> ) notation. The
struct Person{
given code block initializes a Person pointer
type named person1Pointer . Inside the
// member variables
printf() statement, the name member char* name;
variable of person1 is accessed using arrow ( - int age;
> ) notation. };
// `person1` intialization
struct Person person1 = {"Jerry", 29};
// `person1Pointer` intialization to
memory address to `person1`
struct Person* person1Pointer =
&person1;
// accessing `name` through
`person1Pointer`
printf("My name is %s",
person1Pointer->name);
// OUTPUT: My name is Jerry
Passing Structures To Functions
Structures can be used as parameters of functions // Person structure declaration
by using the struct keyword followed by the
struct Person{
structure name in the function de!nition. The given
code block de!nes a function signature named // member variables
myFunc() with a Person parameter named char* name;
person1 . int age;
};
// declaring Person type parameter
void myFunc(struct Person person1);
Passing Structure Pointers To Functions
Structure pointers can be paramters of functions // Person structure declaration
by using the struct keyword, the structure
struct Person{
name, and the pointer symbol ( * ) in the function
de!nition. The given code block de!nes a function // member variables
signature named myFunc() with a Person char* name;
pointer parameter named person1Pointer . int age;
};
// Person pointer parameter
declaration
void myFunc(struct Person*
person1Pointer);
Print Share