CS 31 Discussion, Week 6 Faisal Alquaddoomi, Office Hours: BH 2432, W 4:30-6:30pm, F 12:30-1:30pm.

Slides:



Advertisements
Similar presentations
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.
Advertisements

CS 31 Discussion, Week 9 Faisal Alquaddoomi, Office Hours: BH 2432, W 4:30-6:30pm, F 12:30-1:30pm.
Chapter 10.
COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.
CS31: Introduction to Computer Science I Discussion 1A 5/7/2010 Sungwon Yang
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
Introduction to Computers and Programming Class 22 Character Arrays (Strings) Professor Avi Rosenfeld.
Pointers. Topics Pointers Pointer Arithmetic Pointers and Arrays.
 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.
Chapter 9: Arrays and Strings
1 CS 201 Array Debzani Deb. 2 Having trouble linking math.h? Link with the following option gcc –lm –o test test.o.
Even More C Programming Pointers. Names and Addresses every variable has a location in memory. This memory location is uniquely determined by a memory.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Chapter 8 Arrays and Strings
Computer Science 1620 Strings. Programs are often called upon to store and manipulate text word processors chat databases webpages etc.
 CS105 C++ Lecture 2 Arrays. Parameter passing  2 types  Pass-by-value  Pass-by-reference 2.
CSIS 123A Lecture 6 Strings & Dynamic Memory. Introduction To The string Class Must include –Part of the std library You can declare an instance like.
 2006 Pearson Education, Inc. All rights reserved Arrays.
C Programming Tutorial – Part I CS Introduction to Operating Systems.
Variables, Functions & Parameter Passing CSci 588 Fall 2013 All material not from online sources copyright © Travis Desell, 2011.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT8: Characters and Strings CS2311 Computer Programming.
Chapter 8 Arrays and Strings
Chapter 6 Arrays Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
CS50 Week 2. RESOURCES Office hours ( Lecture videos, slides, source code, and notes (
Array, Pointer and Reference ( I ) Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series.
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
C++ Lecture 3 Monday, 14 July Arrays, Pointers, and Strings l Use of array in C++, multi- dimensional array, array argument passing l Pointers l.
Looping and Counting Lecture 3 Hartmut Kaiser
CS 31 Discussion, Week 8 Faisal Alquaddoomi, Office Hours: BH 2432, W 4:30-6:30pm, F 12:00-1:00pm.
Chapter 13 – C++ String Class. String objects u Do not need to specify size of string object –C++ keeps track of size of text –C++ expands memory region.
Lecture 6 C++ Programming Arne Kutzner Hanyang University / Seoul Korea.
CS 31 Discussion, Week 10 Faisal Alquaddoomi, Office Hours: BH 2432, F 12:30-1:30pm.
POINTERS.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Pointers It provides a way of accessing a variable without referring to its name. The mechanism used for this is the address of the variable.
Review Pointer Pointer Variables Dynamic Memory Allocation Functions.
Strings Jarret Raim C Strings Same as arrays of characters. Use pointers. Require static declarations (compile time). No bounds checking. No easy.
CS 31 Discussion, Week 5 Faisal Alquaddoomi, Office Hours: BH 2432, MW 4:30-6:30pm, F 12:00-1:00pm (today)
Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.
CSE 332: C++ pointers, arrays, and references Overview of Pointers and References Often need to refer to another object –Without making a copy of the object.
Today’s Material Strings Definition Representation Initialization
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
CS 31 Discussion, Week 7 Faisal Alquaddoomi, Office Hours: BH 2432, W 4:30-6:30pm, F 12:30-1:30pm.
Gator Engineering Google Code Jam 2015 Copyright © 2008 W. W. Norton & Company. All rights reserved. 1.
1 Chapter 7 Pointers and C-Strings. 2 Objectives  To describe what a pointer is (§7.1).  To learn how to declare a pointer and assign a value to it.
Lecture 9 – Array (Part 2) FTMK, UTeM – Sem /2014.
CSE 251 Dr. Charles B. Owen Programming in C1 Strings and File I/O.
Slide 1 Chapter 9 Strings. Slide 2 Learning Objectives  An Array Type for Strings  C-Strings  Character Manipulation Tools  Character I/O  get, put.
CS 31 Discussion, Week 2 Faisal Alquaddoomi, Office Hours: BH 2432, MW 4:30-6:30pm, F 12:30-1:30pm (today)
Beginning C for Engineers Fall 2005 Arrays, 2-D arrays, character strings Bettina Schimanski Lecture 5: Section 2 (9/28/05) Section 4 (9/29/05)
Introduction Programs which manipulate character data don’t usually just deal with single characters, but instead with collections of them (e.g. words,
ECE Application Programming
User-Written Functions
CS31 Discussion Jie(Jay) Wang Week6 Nov.4.
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
Motivation and Overview
Arrays in C.
7 Arrays.
Arrays Kingdom of Saudi Arabia
Beginning C for Engineers
7 Arrays.
C++ Pointers and Strings
Arrays Arrays A few types Structures of related data items
CS31 Discussion 1H Fall18: week 6
EECE.2160 ECE Application Programming
4.1 Introduction Arrays A few types Structures of related data items
SPL – PS2 C++ Memory Handling.
Chapter 12: More on C-Strings and the string Class
Presentation transcript:

CS 31 Discussion, Week 6 Faisal Alquaddoomi, Office Hours: BH 2432, W 4:30-6:30pm, F 12:30-1:30pm

Notes and Agenda Slides are now online: Arrays Recap C-Strings 2D Arrays Memory and the Stack – (optional, on board)

Arrays Review Arrays are useful for dealing with large numbers of values Create an array like so, specifying the type, name and size: int myArray[200]; You can also statically allocate arrays, with or without setting the size (it’s set automatically if you leave it out): int myOtherArray[] = { 1, 3, 5, 7, 9, 11 }; Array slots are numbed 0 to length-1, e.g. int someArray[10]; // size 10, slots 0-9

Arrays Review #2 Arrays can be passed to functions – They’re always passed by reference Can prevent modification using “const” modifier – Syntax for taking an array as a parameter: void printAll(const int list[], int len) { for (int i = 0; i < len; i++) { cout << “#” << (i+1); cout << “: “ << list[i] << endl; } Don’t go past the end of an array or before the beginning!

C-Strings A “C-String” is just an array of characters! For instance: char text[10]; // 10 characters As we’ve mentioned, arrays can be of any type, including chars Convenient for representing text when the string class wasn’t around Can be used with cout, can be assigned to strings, etc. (as we’ll describe) But remember that we have trouble with array sizes – e.g. we have to pass them around all the time and be sure not to run off the end or else…

Nul-Termination The solution to the array size problem is nul-termination By convention, a special element that indicates the end of the text in a C-String is a literal 0 – (not the character ‘0’, which is 48) – Referred to in the literature as a ‘nul-terminator’ the ‘nul’ part because it’s 0, and the ‘terminator’ part because it ends the string – You may also see the 0 represented as ‘\0’, which is an escape code that also has the literal value 0 Chosen to allow the length of a C-String to be determined by just examining the elements – Unlike your project, we don’t have to pass around the length of C-Strings (at least if they’re well-formed) – Also implies that a 0 cannot occur within the text itself Also, if the nul-terminator is missing, things will go badly in your life (more on this later)

Creating a C-String Just like any other array, but with some shortcuts (that we’ll see later) Creating a C-String with “Hi!” in it, the hard way: char str[4]; // size 4, slots 0-3 str[0] = ‘H’; str[1] = ‘i’; str[2] = ‘!’; str[3] = 0; // the nul-terminator

Creating a C-String, Cont’d Marginally easier way: char str[4] = {‘H’, ‘i’, ‘!’, ‘\0’}; Even easier: char str[4] = “Hi!”; The above automatically inserts the nul- terminator for us – which is why the char array is still of length 4 – …although it could have been any size >= 4 All of the literal strings you’ve seen so far have been C-Strings all along

C-Strings as Parameters and Arguments Taking a C-String as a parameter: void takesCStr(char str[]) { /* etc */ } – You can use ‘const’ before the ‘char’ in order to prevent it from being modified Passing a C-String as an argument: char myStr[] = “Test”; takeCStr(myStr); Largely the same as arrays (since C-strings are arrays)

Operations with C-Strings Unlike regular strings, you can’t do much with C-Strings without help from some library functions You can do a few operations given this array: char str[] = “Hi!”; Print it: cout << str << endl; Read it: cin >> str; // ‘str’ must be big enough Get a character out of it, or change a character: char p = str[2]; // p == ‘!’ str[0] = ‘P’; // str is now “Pi!” Aside from the printing/reading, pretty much things you’d do with normal arrays…

Things You Can’t do With C-Strings Compare two C-strings: char a[] = “Hello!”; char b[] = {‘H’, ‘e’, ‘l’, ‘l’, ‘o’, ‘!’, ‘\0’}; if (a == b) { /* nope */ } Concatenate two C-Strings: cout << a + b << endl; // nope Assign one C-String to another: a = b; // doesn’t work Check the size of a C-String: a.size(); // won’t work, sadly Thankfully, there are easy ways to do these things…

Library Functions for Strings #include in these key functions: Function SignatureWhat it does int strlen(const char str[]); Returns the length of str, ex: strlen(“Hi”) == 2 char *strcpy(char dest[], const char src[]); Copies src into dest, returns dest char *strcat(char dest[], const char src[]); Sticks src onto the end of dest, returns dest int strcmp(char str1[], char str2[]) Compares two strings, returning 0 if they’re equal

A Brief Notation Note You may occasionally see a character array (or any array) taken as a parameter like so: int strlen(const char *str); – That star means “pointer” in C/C++, but this won’t make any sense if you’re not familiar with memory and addressing – You can simply ignore this and pass an array when you see something that asks for a “char *” (or any other type, e.g. “int *”) for now… I intend to discuss pointers at the end of this lecture, time permitting and interest withstanding

2D Arrays The arrays we discussed previously are technically called ‘1D arrays’ – Have a single ‘dimension’, e.g. myArray[10] has one dimension from 0 to 9 A 2D array is exactly the same as a 1D array in terms of use, except there are two dimensions Used for making tables, grids, matrices, etc. Project 3 used one to hold all the dots that appear on the screen

Creating and Accessing a 2D Array Similar to a 1D array, creating a 2D array: int myArray[10][10]; // ^ has 10*10 == 100 slots Accessing and setting an element: int p = myArray[3][7]; // row 3, col 7 myArray[9][9] = 9000; // ^ sets the last row, col By convention, the first dimension is the row and the second is the column

Statically Allocating a 2D Array Pretty similar to 1D arrays: int multTable[4][4] = { { 1, 2, 3, 4 }, { 2, 4, 6, 8 }, { 3, 6, 9, 12 }, { 4, 8, 12, 16 } }; // multTable[(3-1)][(2-1)] == 6 Unlike 1D arrays, we can only leave out the first dimension if we like; the second is required: multTable[][3] = { { 1, 2, 3 } };

2D Arrays as Params, Arguments Take a 2D array as an argument like so: void myTwoDeeFunc(char input[][5]) { /* etc */ } Note that the second dimension must be specified (like in static allocation of 2D arrays earlier) – There’s a funny reason for this, which you’ll learn if you stick around for memory and the stack (optional) Passing a 2D array is the same as a 1D array: char stuff[3][5]; myTwoDeeFunc(stuff);

Processing an Entire 2D Array Typically uses an outer ‘for’ to go through the rows (#1)… …And an inner ‘for’ to go through the columns for each row (#2) int myArray[10][15]; for (int i = 0; i < 10; i++) { // #1 for (int j = 0; j < 15; j++) { // #2 myArray[i][j] = 0; // row i, col j } } // at the end, all slots are 0

Recap C-Strings are just char arrays, with some convenient functions for manipulating them – End in a ‘nul-terminator’ that signifies the end of the string – Leaving off the nul-terminator is disastrous C-Strings have special manipulation functions in the library, e.g. #include Whenever you use “Hello!” in your code, you’re using a C-String – (but thankfully std::string is good at converting from C-Strings to std::strings when needed)

Recap, Part 2 2D arrays are like 1D arrays, except they have an extra dimension – by convention, the first dimension is the row, the second is the column – declare: int myArray[5][10]; // 5 rows, 10 columns – access/change: myArray[2][3] = 888; // ^ item at row 2, col 3 is now 888 2D arrays can be statically allocated, but the second dimension (columns) must be specified Similarly, when writing a function that takes a 2D array as a parameter, the second dimension must also be specified: void myFunc(char inArray[][5]) { /* body of function here… */ }