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

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.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
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.
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.
The Art of Design Ying Wu Electrical Engineering & Computer Science Northwestern University ECE230 Lectures Series.
 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.
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 Dynamic Memory Allocation Ying Wu Electrical Engineering & Computer Science Northwestern University EECS 230 Lectures Series.
1 Array, Pointer and Reference ( I ) Ying Wu Electrical Engineering and Computer Science Northwestern University EECS 230 Lectures.
 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.
1 Key Concepts:  Data types in C.  What is a variable?  Variable Declaration  Variable Initialization  Printf()  Scanf()  Working with numbers in.
ECE230 Course Introduction Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Introduction to C++ Programming Lecture 3 Arrays & Pointers.
Expressions and Interactivity Chapter 3. 2 The cin Object Standard input object Like cout, requires iostream file Used to read input from keyboard Often.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT8: Characters and Strings CS2311 Computer Programming.
February 11, 2005 More Pointers Dynamic Memory Allocation.
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.
Spring 2005, Gülcihan Özdemir Dağ Lecture 7, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 7 Outline 7. 1.
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.
Function ( I ) Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series.
1 EECS230 Course Introduction and a First Program Ying Wu Electrical Engineering and Computer Science Northwestern University
1 Lecture 12 Pointers and Strings Section 5.4, ,
 2003 Prentice Hall, Inc. All rights reserved. 1 namespaces Program has identifiers in different scopes –Sometimes scopes overlap, lead to problems Namespace.
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.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
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.
 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)
C Arrays.
Introduction to C++ Programming
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:

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

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

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”?

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?

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”?

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

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 ]

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

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] x 10 bytes = 40 bytes –char c[10] x 10 bytes =10 bytes Declaring multiple arrays of same type int b[ 100 ], x[ 27 ];

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

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'

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

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

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?

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!

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

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 }

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

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!