1 / 16

Array, Pointer and Reference ( I )

EECS 230 Lectures Series. Array, Pointer and Reference ( I ). Ying Wu Electrical Engineering and Computer Science Northwestern University yingwu@ece.northwestern.edu. How do you store your data?. We have learnt: Declare a variable A variable in memory stack

Télécharger la présentation

Array, Pointer and Reference ( I )

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  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. What we want … • Input/store a “word”, rather than a “letter”? • Input/store a dataset, rather than a single datapoint? ‘h’ ‘h’ ‘e’ ‘l’ 25 ‘l’ ‘o’ 3.1415

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

  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. 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. String: an Array of chars • 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. 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. 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. 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. 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. cin.getline() • 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. Let’s program! #include <iostream> using std::cin; using std::cout; using std::endl; void main() { // problematic code char a[10]; cout << “\nInput: “; cin >> a; cout << “what you input is: “ << a; // right solution cin.ignore(); char buffer[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. 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. 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!

More Related