Arrays Information Slides
Arrays Information Slides
type id[length];
float averages[200];
int values[10];
3
Defining an Array
Referencing items in an array is
easy and similar to other
languages you may have
encountered previously:
4
What values do you think are in the “empty” slots?
Defining an Array
Referencing items in an array is 🤔
easy and similar to other
languages you may have
encountered previously: 🤔
🤔
🤔
🤔
5
Initializing Arrays
Just as you can assign initial int counters[5] = { 0, 0, 0, 0, 0 };
const (constant). pi = pi / 2;
● Changing the value of a
const results in a compiler
error.
● In addition to the software
engineering benefit, it also
allows the compiler to place
the value of a constant in
read-only memory,
optimize based on its value,
etc., making your code 13
Multidimensional Arrays
int M[4][5]; // M[i][j]
M[2][1] = M[0][0] * 2;
14
Multidimensional Arrays
int M[4][5]; // M[i][j]
M[2][1] = M[0][0] * 2;
int M[4][5] = {
{ 10, 5, -3, 17, 82 },
{ 9, 0, 0, 8, -7 },
{ 32, 20, 1, 0, 14 },
{ 0, 0, 8, 7, 6 }
}; 15
Multidimensional Arrays
int M[4][5]; // M[i][j]
M[2][1] = M[0][0] * 2;
int M[4][5] = {
10, 5, -3, 17, 82,
9, 0, 0, 8, -7,
32, 20, 1, 0, 14,
0, 0, 8, 7, 6
}; 16
Multidimensional Arrays
int M[4][5]; // M[i][j]
M[2][1] = M[0][0] * 2;
int M[4][5] = {
{ 10, 5, -3 },
{ 9, 0, 0 },
{ 32, 20, 1 },
{ 0, 0, 8 }
}; 17
Multidimensional Arrays
int M[4][5]; // M[i][j]
M[2][1] = M[0][0] * 2;
int M[4][5] = {
[0][0] = 10,
[0][1] = 5,
[0][2] = -3
};
18
Multidimensional Arrays
int M[4][5]; // M[i][j]
22
Array Length, Iteration, Bounds Checking
Thus, if we define a function
that must iterate over an
array, we must tell it the
length of the given array.
23
Function
Defining a Function
Defining functions in C is
easy.
25
Arguments and Local Variables
It is also easy to
declare local
variables.
26
Function Prototype Declaration
This is referred to as
the function
prototype
declaration.
29
Carnegie Mellon
Example Stack
yoo(…)
yoo(…) yoo %ebp
{{ yoo
who yoo
•• %esp
••
amI amI
who();
who();
•• amI
••
}}
amI
Course feedback:
https://bit.ly/
2klMxqH
Carnegie Mellon
Example Stack
yoo(…)
yoo(…) yoo
{{ who(…)
who(…) yoo
yoo
•{
•{ who
%ebp
•• •• •• •• who
amI();
amI();
who(); amI amI
who(); %esp
•• •• •• ••
•• amI();
amI(); amI
}} •• •• ••
}} amI
Course feedback:
https://bit.ly/
2klMxqH
Carnegie Mellon
Example Stack
yoo(…)
yoo(…) yoo
{{ who(…)
who(…) yoo
yoo
•{
•{ amI(…)
amI(…) who
•• {•{ •• ••
•
who
amI();
amI();
who(); •• amI amI
who();
•• •• •••• •• %ebp
amI amI
•• amI();
amI();
amI();
amI();
•
• ••• ••
• %esp
}}
}} •• amI
}}
Course feedback:
https://bit.ly/
2klMxqH
Carnegie Mellon
Example Stack
yoo(…)
yoo(…) yoo
{{ who(…)
who(…) yoo
yoo
•{
•{ amI(…)
amI(…) who
•• {•{ •• amI(…)
• ••
amI(…) who
amI();
amI();
who(); • amI amI
who(); •{{
•• • ••• •• ••
• •
amI amI
•• amI();
amI();
amI();
amI();
••
}} • • •
• ••• • amI();
amI(); %ebp
}} •• • amI amI
•
}} •• %esp
}}
Course feedback:
https://bit.ly/
2klMxqH
Carnegie Mellon
Example Stack
yoo(…)
yoo(…) yoo
{{ who(…)
who(…) yoo
yoo
•{
•{ amI(…)
amI(…) who
•• {•{ •• amI(…)
• ••
amI(…) who
amI();
amI();
who(); • amI amI
who(); •{{ amI(…)
amI(…)
•• • ••• •• ••{
• •
{ amI amI
•• amI();
amI();
amI();
amI();
•• •
•• ••• •• •
}} • amI();
amI();
••
}} •• • amI amI
• amI();
amI();
}} •• •
• %ebp
}} amI
••
}} %esp
Course feedback:
https://bit.ly/
Carnegie Mellon
Example Stack
yoo(…)
yoo(…) yoo
{{ who(…)
who(…) yoo
yoo
•{
•{ amI(…)
amI(…) who
•• {•{ •• amI(…)
• ••
amI(…) who
amI();
amI();
who(); • amI amI
who(); •{{
•• • ••• •• ••
• •
amI amI
•• amI();
amI();
amI();
amI();
••
}} • • •
• ••• • amI();
amI(); %ebp
}} •• • amI amI
•
}} •• %esp
}}
Course feedback:
https://bit.ly/
2klMxqH
Carnegie Mellon
Example Stack
yoo(…)
yoo(…) yoo
{{ who(…)
who(…) yoo
yoo
•{
•{ amI(…)
amI(…) who
•• {•{ •• ••
•
who
amI();
amI();
who(); •• amI amI
who();
•• •• •••• •• %ebp
amI amI
•• amI();
amI();
amI();
amI();
•
• ••• ••
• %esp
}}
}} •• amI
}}
Course feedback:
https://bit.ly/
2klMxqH
Carnegie Mellon
Example Stack
yoo(…)
yoo(…) yoo
{{ who(…)
who(…) yoo
yoo
•{
•{ who
%ebp
•• •• •• •• who
amI();
amI();
who(); amI amI
who(); %esp
•• •• •• ••
•• amI();
amI(); amI
}} •• •• ••
}} amI
Course feedback:
https://bit.ly/
2klMxqH
Carnegie Mellon
Example Stack
yoo(…)
yoo(…) yoo
{{ who(…)
who(…) yoo
yoo
•{
•{ amI(…)
amI(…) who
•• {•{ •• ••
•
who
amI();
amI();
who(); •• amI amI
who();
•• •• •••• •• %ebp
amI amI
•• amI();
amI();
amI();
amI();
•
• ••• ••
• %esp
}}
}} •• amI
}}
Course feedback:
https://bit.ly/
2klMxqH
Carnegie Mellon
Example Stack
yoo(…)
yoo(…) yoo
{{ who(…)
who(…) yoo
yoo
•{
•{ who
%ebp
•• •• •• •• who
amI();
amI();
who(); amI amI
who(); %esp
•• •• •• ••
•• amI();
amI(); amI
}} •• •• ••
}} amI
Course feedback:
https://bit.ly/
2klMxqH
Carnegie Mellon
Example Stack
yoo %ebp
yoo(…)
yoo(…) yoo
{{ who yoo
%esp
••
•• amI amI
who();
who();
•• amI
••
}} amI
Course feedback:
https://bit.ly/
2klMxqH
Application
Functions and Arrays
● Passing an array to a int minimum(int v[100]) {
function: you only ...
need to provide the return min_value;
array name. }
42
Functions and Arrays
● Passing an array to a int minimum(int v[100]) {
function: you only need ...
to provide the array return min_value;
name. }
43
Functions and Arrays
● Passing an array to a int minimum(int v[]) {
function you only need ...
to provide the array return min_value;
name. }
● An array is mutable
(can be affected by a
callee).
int counter = 0;
A variable that is not declared
locally to a function is global void tally() { counter++; }
to the file it is declared in.
int main() {
In other words, all functions
tally(); tally(); tally();
declared later in the same file
printf(“%i\n”, counter);
have access to that function.
return 0;
}
51
Variable Length Character Strings
52
Variable Length Character Strings
The C compiler will automatically insert the null character at the end.
(Note: these are double quotes, not the single quotes that go around
individual characters.)
54
Variable Length Character Strings
The C compiler will automatically insert the null character at the end.
Furthermore, you can omit the braces:
The C compiler will automatically insert the null character at the end.
Furthermore, you can omit the braces: You can also print strings
out:
char word[] = “Hello!”;
printf(“%s\n”, word);
And there you have it, the C string is born!
56
String Equality
Unfortunately, this does not work:
if (string1 == string2) { … }
58
The Null String
A character string that contains no characters other than the null
character has a special name in the C language; it is called the null
string.
Always remember that the null string does, in fact, have a character in
it, albeit a null one.
The above defines a character array of 5 items with the first set to null.
[’\0’, ?, ?, ?, ?]
59
Escape Characters
There are all
sorts of escape
characters that
can be used in
strings:
60
Constant Strings
You can have multi-line strings:
61
Constant Strings
You can have multi-line strings:
63
10-sec Q &A
Raise your hand if it is a string
char letter[] = “abcdefghijklmnopqrstuvwxyz”
“ABCDEFGHIJKLMNOPQRSTUVWXYZ”;
65
Raise your hand if it is a string
char letter[] = {‘h’, ‘e’, ‘l’, ‘l’, ‘o’};
66
Raise your hand if it is a string
char letter[] = {‘h’, ‘e’, ‘l’, ‘l’, ‘o’, ‘\n’};
67
Raise your hand if it is a string
char letter[] = {‘\0’};
68
Raise your hand if the size of array is equal to
5 chars
char buffer[5] = ””;
69
Raise your hand if the size of array is equal to
5 chars
char buffer[5] = ”hello”;
70