Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Array, Pointer and Reference ( I ) Ying Wu Electrical Engineering and Computer Science Northwestern University EECS 230 Lectures.

Similar presentations


Presentation on theme: "1 Array, Pointer and Reference ( I ) Ying Wu Electrical Engineering and Computer Science Northwestern University EECS 230 Lectures."— Presentation transcript:

1 1 Array, Pointer and Reference ( I ) Ying Wu Electrical Engineering and Computer Science Northwestern University yingwu@ece.northwestern.edu EECS 230 Lectures Series

2 2 How do you store your data? We have learnt: –Declare a variable –A variable in memory stack –Get input and store the value in a variable –Output the value of a variable int a; cin >> a; cout << “the value of a is: “ << a << endl; char c; cin >> c; cout << “the value of c is: “ << c << endl; cout << “the value of c is: “ << (int)c << endl; Question: what if I want to input/store a data set?

3 3 What we want … Input/store a “word”, rather than a “letter”? Input/store a dataset, rather than a single datapoint? ‘h’ ‘e’ ‘l’ ‘o’ 25 3.1415

4 4 Good news! Array –Consecutive group of memory locations –Same name and type To refer to an element, specify –Array name and position number Format: arrayname[ position number ] –First element is located at position 0 –n element array c : c[ 0 ], c[ 1 ] … c[ n - 1 ] Array elements are like normal variables c[ 0 ] = 3; cout << c[ 0 ]; Performing operations in subscript. If x = 3, c[ 5 – 2 ] == c[ 3 ] == c[ x ]

5 5 Core Concept of Array c[6] -45 6 0 72 1543 -89 0 62 -3 1 6453 78 Name of array (Note that all elements of this array have the same name, c) c[0] c[1] c[2] c[3] c[11] c[10] c[9] c[8] c[7] c[5] c[4] Position number of the element within array c

6 6 Declaring Arrays Declaring an array: –Name? Type? Number of elements? –Examples int c[ 10 ]; float hi[ 3284 ]; What will happen once you declare an array? –The O/S will allocate (reserve) a group (consecutive) of memory units for this array. You need to specify the # of elements Once the #of elements is specified, you can not change it. Why? … Let’s see how many memory are allocated: int a[10] ------- 4 x 10 bytes = 40 bytes char c[10] ------- 1 x 10 bytes =10 bytes Declaring multiple arrays of same type int b[ 100 ], x[ 27 ];

7 7 Initializing an Array Initializers int n[ 5 ] = { 1, 2, 3, 4, 5 }; –If not enough initializers, rightmost elements are set to be 0. –If too many initializers, a syntax error is generated int n[ 5 ] = { 0 } –Sets all the elements to 0 If size omitted, the initializers determine it int n[] = { 1, 2, 3, 4, 5 }; –5 initializers, therefore n is a 5 element array

8 8 String: an Array of char s Strings –Arrays of characters (including letters, digits, special characters +, -, * …) –How do we know the end of a string? All strings end with NULL ( '\0' ) –Examples: char string1[] = "hello"; char string1[] = { 'h', 'e', 'l', 'l', 'o‘, '\0’}; –Subscripting is the same as for a normal array string1[ 0 ] is 'h' string1[ 2 ] is 'l'

9 9 Confusion: ‘a’  a, “a”  ‘a’ Question: ‘a’ ?= a –Answer: NO, unless … –Why? ‘a' means the letter a, which is 97, numerically. Question: “a” ?= ‘a’ –Answer: NO, NO –Why? “a” means a string, i.e., a set of characters “a” actually is ‘a’ and ‘\0’ So, size(“a”) is 2 bytes, while size(‘a’) is 1 byte So, to hold a word with length N, you need a string of N+1 bytes

10 10 Initializing a String String assignment –Character array: char color[] = "blue"; –What is the # of elements of color? 4 or 5? Answer: Creates 5 element char array, color, (last element is '\0' )

11 11 Input a sentence? Assign input to character array word[20] cin >> word –Reads characters until whitespace or EOF Question: how many chars can word hold? –Answer: 20 or 19? Question: what if I input more than enough? –Answer: exceed array size, and it may crash your program! Question: how to solve this problem? –Answer: cin >> setw(20) >> word Question: can this method read a sentence? –Answer: NO Question: so, how can I solve it?

12 12 A Closer look at cin/cout cin/cout work with strings or char arrays char str[10]; cin >> str; cout << str; But … –cin keeps getting data until it meets a space, a return or a ‘\0’ If I input more char than the size of the array, cin will still keep inputting, while it may be DANGEOUS! –cout keeps outputting data until it meets a ‘\0’ It does not check the size of the array! If there is no ‘\0’ in the array, what can you imagine? So, be careful!

13 13 cin.getline() –Prototype: cin.getline( array, size, delimiter character); –Copies input into specified array until either One less than the size is reached The delimiter character is input –Examples char sentence[ 80 ]; cin.getline( sentence, 80); cin.getline( sentence, 80, '\n' );

14 14 Let’s program! #include using std::cin; using std::cout; using std::endl; void main() { // problematic code chara[10]; cout << “\nInput: “; cin >> a; cout << “what you input is: “ << a; // right solution cin.ignore(); charbuffer[500]; cout << "\nInput a command line: "; // prompt for input cin.getline(buffer, 500); // get input from keyboard cout << “what you input is: “ << buffer << endl; // echo }

15 15 What have we learnt today? The core concept of array An array in memory Initializing an array String – char array Why string is quite special? cin/cout cin.getlint( )

16 16 Q. for today: A Bad Example int main( ) { int size; cin >> size; int arr[size] for(int i=0;i<size;i++) arr[i] = i; return 0; } I know what you want to do. But this method won’t work. So, how can we do that? Keep it and we will see in later lectures!


Download ppt "1 Array, Pointer and Reference ( I ) Ying Wu Electrical Engineering and Computer Science Northwestern University EECS 230 Lectures."

Similar presentations


Ads by Google