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

Slides:



Advertisements
Similar presentations
EC-111 Algorithms & Computing Lecture #11 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Advertisements

 2003 Prentice Hall, Inc. All rights reserved Fundamentals of Characters and Strings Character constant –Integer value represented as character.
Strings.
Current Assignments Homework 5 will be available tomorrow and is due on Sunday. Arrays and Pointers Project 2 due tonight by midnight. Exam 2 on Monday.
C Strings. The char Data Type for Storing Characters The char data type can is used to declare a variable that can hold a single character. Examples:
Pointers and Strings. Introduction Pointers –Powerful, but difficult to master –Simulate call-by-reference –Close relationship with arrays and strings.
Arrays. Introduction Arrays –Structures of related data items –Static entity - same size throughout program A few types –C-like, pointer-based arrays.
1 Array, Pointer and Reference ( III ) Ying Wu Electrical Engineering & Computer Science Northwestern University ECE230 Lectures.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Arrays Outline 4.1Introduction 4.2Arrays 4.3Declaring Arrays 4.4Examples Using Arrays 4.5Passing.
 2003 Prentice Hall, Inc. All rights reserved Introduction Arrays –Structures of related data items –Static entity (same size throughout program)
CS31: Introduction to Computer Science I Discussion 1A 5/7/2010 Sungwon Yang
1 String Library and Stream I/O Ying Wu Electrical Engineering & Computer Science Northwestern University ECE230 Lectures Series.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
Array, Pointer and Reference ( V ) Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series.
1 Midterm Review Ying Wu Electrical Engineering & Computer Science Northwestern University EECS230 Lectures Series.
1 Dynamic Memory Allocation Ying Wu Electrical Engineering & Computer Science Northwestern University EECS 230 Lectures Series.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Introduction to C++ Programming Lecture 3 Arrays & Pointers.
 Write a program that uses a one dimension to do a table look-up  Learn about parallel arrays.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT8: Characters and Strings CS2311 Computer Programming.
CHAPTER 07 Arrays and Vectors (part I). OBJECTIVES 2 In this part you will learn:  To use the array data structure to represent a set of related data.
Chapter 6 Arrays Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
 2000 Prentice Hall, Inc. All rights reserved Arrays Array –Group of consecutive memory locations –Same name and type To refer to an element, specify.
Array, Pointer and Reference ( I ) Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series.
CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Dynamic memory allocation and Pointers Lecture 4.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays Outline Introduction Arrays Declaring Arrays Examples Using Arrays.
 2008 Pearson Education, Inc. All rights reserved Pointers and Pointer-Based Strings.
 2000 Deitel & Associates, Inc. All rights reserved Introduction Pointers –Powerful, but difficult to master –Simulate call-by-reference –Close.
1 Chapter 4 - Arrays Outline 4.1Introduction 4.2Arrays 4.3Declaring Arrays 4.4Examples Using Arrays 4.5Passing Arrays to Functions 4.6Sorting Arrays 4.7Case.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 4 - Arrays Outline 4.1Introduction 4.2Arrays 4.3Declaring Arrays 4.4Examples Using Arrays.
Pointers A pointer is a variable that contains a memory address as it’s value. The memory address points to the actual data. –A pointer is an indirect.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Pointers *, &, array similarities, functions, sizeof.
1 Lecture 12 Pointers and Strings Section 5.4, ,
CHAPTER 6 ARRAYS IN C++ 2 nd Semester King Saud University College of Applied studies and Community Service CSC 1101 By: Fatimah Alakeel Edited.
 2003 Prentice Hall, Inc. All rights reserved. 1 namespaces Program has identifiers in different scopes –Sometimes scopes overlap, lead to problems Namespace.
C++ Programming Basics
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
 2003 Prentice Hall, Inc. All rights reserved. 5.11Function Pointers Pointers to functions –Contain address of function –Similar to how array name is.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 5 - Pointers and Strings Outline 5.1 Introduction 5.2 Pointer Variable Declarations and Initialization.
 2003 Prentice Hall, Inc. All rights reserved. 11 Fundamentals of Characters and Strings Character constant –Integer value of a character –Single quotes.
Review Pointer Pointer Variables Dynamic Memory Allocation Functions.
C++ Programming Lecture 19 Strings The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
1 Lecture 8 Pointers and Strings: Part 2 Section 5.4, ,
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT:10 Advance Pointer Array, String and Dynamic Memory Allocation CS2311 Computer Programming.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Arrays Outline 4.1Introduction 4.2Arrays 4.3Declaring Arrays 4.4Examples Using Arrays 4.5Passing.
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Arrays Outline 6.1Introduction 6.2Arrays 6.3Declaring.
CHAPTER 3 ARRAYS Dr. Shady Yehia Elmashad. Outline 1.Introduction 2.Arrays 3.Declaring Arrays 4.Examples Using Arrays 5.Multidimensional Arrays 6.Multidimensional.
CHAPTER 6 ARRAYS IN C 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 F. Alakeel.
 2000 Prentice Hall, Inc. All rights reserved Arrays Array –Consecutive group of memory locations –Same name and type To refer to an element, specify.
EC-111 Algorithms & Computing Lecture #10 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
4.1Introduction Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based arrays (C-like) –Arrays.
Fundamentals of Characters and Strings
CSC 113: Computer Programming (Theory = 03, Lab = 01)
Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays
Chapter 9 - Arrays Outline 6.1 Introduction 6.2 Arrays
Arrays Kingdom of Saudi Arabia
Pointers and Pointer-Based Strings
Capitolo 4 - Arrays Outline 4.1 Introduction 4.2 Arrays
Arrays Arrays A few types Structures of related data items
Lecture 2 Arrays & Pointers May 17, 2004
C++ Programming Lecture 20 Strings
Chapter 3 Arrays Dr. A. PHILIP AROKIADOSS Assistant Professor
Lecture 2 Arrays & Pointers September 7, 2004
Data Structure(s) A way of storing and organizing data in a computer so that it can be used efficiently. e.g. Arrays Linked Lists stacks Queues Trees.
4.1 Introduction Arrays A few types Structures of related data items
Presentation transcript:

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

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’ ‘e’ ‘l’ ‘o’

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 c[6] 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 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] x 10 bytes = 40 bytes char c[10] 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 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 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() –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 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 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!