Arrays, Vectors, and Strings Allocation and referencing.

Slides:



Advertisements
Similar presentations
Etter/Ingber Arrays and Matrices. Etter/Ingber One-Dimensional Arrays 4 An array is an indexed data structure 4 All variables stored in an array are of.
Advertisements

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.
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
Chapter 10.
. Plab – Tirgul 2 Const, C Strings. Pointers int main() { int i,j; int *x; // x points to an integer i = 1; x = &i; j = *x; ijx 1.
C++ Spring 2000 Arrays1 C++ Arrays. C++ Spring 2000 Arrays2 C++ Arrays An array is a consecutive group of memory locations. Each group is called an element.
CS31: Introduction to Computer Science I Discussion 1A 5/7/2010 Sungwon Yang
Arrays Hanly - Chapter 7 Friedman-Koffman - Chapter 9.
Pointers. Topics Pointers Pointer Arithmetic Pointers and Arrays.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
 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.
Pointers: Part I. Why pointers? - low-level, but efficient manipulation of memory - dynamic objects  Objects whose memory is allocated during program.
Chapter 9: Arrays and Strings
C++ for Engineers and Scientists Third Edition
Lesson 7 Arrays CS 1 Lesson 7 -- John Cole1. Arrays Hold Multiple Values Array: variable that can store multiple values of the same type Values are stored.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Arrays and Strings Gabriel Hugh Elkaim Spring 2013.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
 2006 Pearson Education, Inc. All rights reserved Arrays.
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.
CMSC 202 Arrays. Aug 6, Introduction to Arrays An array is a data structure used to process a collection of data that is all of the same type –An.
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
Arrays & Vectors Week 5. The simplest form of the multidimensional array is the two-dimensional array. A two- dimensional array is, in essence, a list.
CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
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.
Pointers. What is pointer l Everything stored in a computer program has a memory address. This is especially true of variables. char c=‘y’; int i=2; According.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
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.
1 Arrays and Vectors Chapter 7 Arrays and Vectors Chapter 7.
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
1 Chapter 7 Arrays. 2 Topics 7.1 Arrays Hold Multiple Values 7.2 Accessing Array Elements 7.3 No Bounds Checking in C Array Initialization 7.5 Processing.
String Class. C-style and C++ string Classes C-style strings, called C-strings, consist of characters stored in an array ( we’ll look at them later) C++
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
Arrays. Related data items Collection of the same types of data. Static entity – Same size throughout program.
Introduction to C++ Part II Version 1.3. Topics C++ Functions -- passing by value in C++ -- passing by reference in C++ -- passing by address in C++ C++
12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter Chapter 6 One-Dimensional Arrays.
1 Character Strings (Cstrings) Reference: CS215 textbook pages
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
String Class Mohamed Shehata 1020: Introduction to Programming.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 7 Arrays.
1 Chapter 12 Arrays. 2 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating.
Review Pointer Pointer Variables Dynamic Memory Allocation Functions.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT:10 Advance Pointer Array, String and Dynamic Memory Allocation CS2311 Computer Programming.
Chapter 16 Pointers and Arrays Pointers and Arrays We've seen examples of both of these in our LC-3 programs; now we'll see them in C. Pointer Address.
Chapter Nine Strings. Char vs String Literals Size of data types: Size of data types: –sizeof(“hello\n”)7 bytes –sizeof(“hello”)6 bytes –sizeof(“X”)2.
1 ENERGY 211 / CME 211 Lecture 4 September 29, 2008.
Compound Types: References, Pointers Where are we going with this?
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.
1 Principles of Computer Science I Honors Section Note Set 3 CSE 1341.
Arrays An array is a sequence of objects all of which have the same type. The objects are called the elements of the array and are numbered consecutively.
Pointers Lecture: 5. Topics 1 Pointers and the Address Operator 2 Pointer Variables 3 The Relationship Between Arrays and Pointers 4 Pointer Arithmetic.
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
Strings.
CS31 Discussion Jie(Jay) Wang Week6 Nov.4.
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
Vectors.
7 Arrays.
Pointers, Dynamic Data, and Reference Types
7. 11 Introduction to C++ Standard Library Class Template vector (Cont
String class and its objects
7 Arrays.
Arrays Arrays A few types Structures of related data items
Today’s Objectives 28-Jun-2006 Announcements
character manipulation
4.1 Introduction Arrays A few types Structures of related data items
Presentation transcript:

Arrays, Vectors, and Strings Allocation and referencing

Array Type Arrays are a compound type Base_type name[size]; Array dimension specifies the number of elements Dimension must be a positive integer literal OR a constant expression (i.e., known at compile time)

Array Examples cnt = 100;// not a const expr constexpr max = 100;// a const expr int array1[100];// good int array2[max];// good int array3[cnt];// error – not const int array4[getsize(something)]; // good iff getsize() is constexpr

Arrays Array indices run from 0 to Max-1 Always check your index!! Can reference element by index or by pointer dereference A[i] or *(A + i) Type allows compiler to compute correct offset into memory

0xfa10 0xfa58 0xfa54 0xfa50 0xfa4c 0xfa48 0xfa44 0xfa40 0xfa3c 0xfa38 0xfa34 0xfa30 0xfa2c 0xfa28 0xfa24 0xfa20 0xfa1c 0xfa18 0xfa14 AddressRAM 0x int array1[5]={1,2,4,8,16}; char name[5]=“Mary”; int *ptr = &array1[0]; 0x x x x ‘y’ ‘a’ ‘r’ ‘M’ ‘\0’ ‘\0’ Symbol Table IdentifierTypeLocation array1int*0xfa18 namechar*0xfa2c ptrint*0xfa18 Note: null terminated Arrays

2-D Arrays C++ does not really have multi-D arrays Looks kind of like it: int A[M][N]; Arrays are really pointers to the first element in a consecutively stored sequence of elements of same type 2-D array is really pointer to a 1-D array of pointers to first row elements

C-Style Strings C++ has a String class Can be referenced by index like array But it is a true object C strings are not a primitive type, nor are they a struct A C-style string is just a 1-D array of char, with NULL termination NOTA BENE: always a '\0' at end!!!!

0xfa10 0xfa58 0xfa54 0xfa50 0xfa4c 0xfa48 0xfa44 0xfa40 0xfa3c 0xfa38 0xfa34 0xfa30 0xfa2c 0xfa28 0xfa24 0xfa20 0xfa1c 0xfa18 0xfa14 AddressRAM 0x char names[3]={“Julian”, “James”,“John”}; 0x x x x ‘y’ ‘a’ ‘r’ ‘M’ ‘\0’ ‘\0’ Symbol Table IdentifierTypeLocation array1int*0xfa18 namechar*0xfa2c ptrint*0xfa18 2-D Arrays names char*[] 0xfa34 0x00000fa40 0x0000fa47 0x0000fa4d ‘i’ ‘l’ ‘u’ ‘J’ ‘J’ ‘\0’ ‘n’ ‘a’ ‘s’ ‘e’ ‘m’ ‘a’ ‘h’ ‘o’ ‘J’ ‘\0’ ? ? ‘\0’ ‘n’

C-Style Strings and Chars Remember, char and string are not the same 'a' is a char literal – uses one byte of RAM “a” is a string literal – uses two bytes of RAM Name[] = “Joe”; - uses... 4 bytes for the 3 characters plus null character Char *name – allocate pointer ONLY!! library – many functions

C String Library #include i=strlen(p);// string length less null i=strcmp(p1,p2);// neg if p1<p2, etc. p1=strcat(p1,p2);// appends p2 to p1 p1=strcpy(p1,p2);// copies p2 to p1 /*** WARNING – NO BOUNDS CHECKING! ***/ /* use these safe versions below! */ i=strncmp(p1,p2,n);// … only up to n p1=strncat(p1,p2,n);// … only up to n p1=strncpy(p1,p2,n);// … only up to n

C String Library #include size_t strlen(char s[])// not int! int atoi(char s[])// int value of s[] double atof(char s[])// double value long atol(char s[])// long value void itoa(int val, char s[], int radix) // converts integer value to string

C++ Strings C++ has string type #include using std::string; string s1;// default to empty string s2=s1;// s2 is a *copy* of s1 string s3=“Joe”;// s3 is copy of literal string s4=(10,’c’);// s4 is cccccccccc while (cin >> word) …// whitespace delim while (getline(cin, line)) … // \n delim

Strings & C-Strings string s(“Hello World”); char *str=s;// error! const char *str = s.c_str();// OK /* Achtung! Contents of *str may change * Copy into local version if need * continued access to contents */ Use s.insert(), s.erase(), s.assign(), s.append(), and s.replace() on strings

Vectors Vectors very similar to arrays Except they are class templates, not a type – must include type in declaration Take care of memory management Use push_back() to expand

Vector Initialization vector dvec;// ivec empty vector ivec(10, 5); /* 10 ints all 10 have value 5 */ vector ivec2(ivec); // copy ivec vector ivec3 = ivec;// also copy vector vec4(10);/* 10 item vector of type T objs default init */ vector ivec5{2,3,5,7,11,13} // 6 elements with values given vector ivec6 = {1,2,4,8,16} // 5 elements with values given

Vector Initialization vector ivec1(10); vector ivec2{10}; vector ivec3(10, 5); vector ivec4{10, 5}; vector sv1{“Al”,“Mo”, “Jo”}; vector sv2(“Al”,“Mo”,“Jo”); // 10 ints all value 0 // one int value 10 // 10 ints all value 5 // 2 ints, values 10 & 5 // list initialization // error

Adding to a Vector Vector size may not be known Vector may be large Use push_back member function vector iv; for (int i=0; i != MAX; ++i) iv.push_back(i*i); vector text; while (cin >> word) // word is string text.push_back(word); Subscripting does not add elements!

Range For Auto type definition if unknown Range for – like “foreach” vector iv2{1,2,3,4,5,6,7,8,9}; for (auto &i=0 : iv2) // reference! i *= i;// square elements for (auto i : iv2)// non-reference cout << i << endl;