Presentation is loading. Please wait.

Presentation is loading. Please wait.

Array, Pointer and Reference ( I ) Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series.

Similar presentations


Presentation on theme: "Array, Pointer and Reference ( I ) Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series."— Presentation transcript:

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

2 The GOAL for Week-1/2 Understand all C/C++ data types Understand basic computer architecture and addressing mechanism Use cin/cout to input/output Grasp six C/C++ control structures Understand array Design by using flowcharts Get a sense of the compiling process DEBUG_1: debugging when you are coding Do the MP#1: A Command Line Partitioner

3 MP#1: Command Partitioning MP#1 description MP#1 demo So, to do that, what shall we learn? Get input from users? Output and display stuff on the screen? Store the “stuff” in computer? Retrieve the “stuff” from the computer? How to find a “piece”?

4 What to learn today? Get the input of a whole command line? Before answering it, we should ask: –Where to put the command line input? –How does it look like in the memory stack? –What is an array? –What is a string? –How to end a string?

5 Where to store your inputs? 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 “word”?

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

7 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 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 ]

8 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

9 Declaring Arrays Declaring arrays - specify: –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 ];

10 Initializing an Array Initializers int n[ 5 ] = { 1, 2, 3, 4, 5 }; –If not enough initializers, rightmost elements become 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

11 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'

12 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

13 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' )

14 Input a word? 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: is this what we want for MP#1? –Answer: NO Question: so, how to solve it?

15 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!

16 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' );

17 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 }

18 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( )

19 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 "Array, Pointer and Reference ( I ) Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series."

Similar presentations


Ads by Google