0% found this document useful (0 votes)
58 views1 page

Arrays and Strings: Array: Which Stores

The document discusses arrays and strings in C. Arrays allow storing multiple values of the same type together, accessed via indexes. Strings are represented as arrays of characters, with a null terminator at the end. Loops can be used to iterate through arrays and strings.

Uploaded by

Eryz Riansyah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views1 page

Arrays and Strings: Array: Which Stores

The document discusses arrays and strings in C. Arrays allow storing multiple values of the same type together, accessed via indexes. Strings are represented as arrays of characters, with a null terminator at the end. Loops can be used to iterate through arrays and strings.

Uploaded by

Eryz Riansyah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

CS50 Arrays and Strings

Overview Key Terms


Recall that variables are used to store values. Quite frequently, we may want to use • array
multiple variables to store a sequence of values: like a sequence of 10 test scores, or 50
• string
addresses. For situations like these, C has a data structure called an array: which stores
• size
multiple values of the same type of data. For instance, an array of ints would store
multiple int values back-to-back. The string type that you have been using is really just • index
an array of chars. • null-terminator

1 int ages[5];
0 1 2 3 4
Arrays
Like variables, arrays are declared by first stating the type of the
data to be stored, followed by the name of the array. In brackets
after the name of the array is the size of the array: which defines
how many values the array will hold. For example, line 1 at left de-
2 ages[0] = 28; clares an array of 5 ints.
0 1 2 3 4
28 You can visualize an array as a sequence of boxes, each one hold-
ing a value, and each one with a numbered index, which is a num-
ber that can be used to access a specific value in an array. In C,
3 ages[1] = 15; arrays are zero-indexed, meaning that the first item in an array has
4 ages[2] = ages[1]; index 0, the second item has index 1, etc.
5 ages[3] = ages[1] - 1;
6 ages[4] = 17; To access a particular value in an array, use the name of the array,
0 1 2 3 4 followed by the desired index in brackets. Line 2 at left sets the
value of the first item in the ages array (the one at index 0) to 28.
28 15 15 14 17
The value at each array index can be treated like a normal variable.
7 for (int i = 0; i < 5; i++) For example, you can change its value, apply arithmetic or assign-
8 { ment operators to it.
9 ages[i] += 1;
10 } Since each value in an array is referenced by its index number, it's
0 1 2 3 4 easy to loop through an array. Lines 7 through 10 define up a for
loop, which iterates through the entire array, and increases each
29 16 16 15 18 age value by 1.

Strings
In C, a string is represented as an array of char values. Thus, when
string s = "CS50";
we write a line like string s = "CS50";, this information is stored as
an array of chars, with one character at each index. The final index of 0 1 2 3 4
a string in C is the null-terminator, represented by '\0'. The null-ter-
minator is the character that tells a string that the string is over, and
'C' 'S' '5' '0' '\0'
that there are no more characters in the string.

Since a string is just an array, you can index into the string just like you would index into any other array
in order to access the value of a particular character. For instance, in the example above, indexing into s[0]
would give you the character 'C', the first character in the string "CS50".

This also makes it very easy to use a loop to interate through a string and perform computation on each in-
dividual character within a string, by first initializing the loop counter to 0, and repeating until the last index
of the string. The function strlen takes in a string as input, and returns the length of the string as an integer,
which may help in determining how many times the loop should repeat.
© 2017 This is CS50.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy