C++ Strings Pointer Basics - Midterm Exam Review | CISC 181, Exams of Computer Science

Material Type: Exam; Class: Introduction to Computer Science II; Subject: Computer/Information Sciences; University: University of Delaware; Term: Unknown 1989;

Typology: Exams

Pre 2010

Uploaded on 09/02/2009

koofers-user-y3i-1
koofers-user-y3i-1 🇺🇸

10 documents

1 / 15

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
C++ strings
pointer basics
midterm overview
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download C++ Strings Pointer Basics - Midterm Exam Review | CISC 181 and more Exams Computer Science in PDF only on Docsity!

C++ strings

pointer basics

midterm overview

Last class

Character stuff

old code treats as int or byte

character manipulation -

C strings

how are C strings stored?

how to declare a C string and variable?

C++ strings

string class in with namespace std string str1, str2(“hi”);

can pass a C string to the constructor

takes care of the array size details so you don’t have to

basically a wrapper class - it effectively has a C string inside of it

C++ strings

overloaded operators

  • concatenates two string objects (but recall automatic type conversion)

= copies the rvalue into the lvalue (and may resize the underlying C string in the lvalue)

and << work the same as C strings

C++ strings

overloaded []

can treat them like char[] if you want

doesn’t do bounds checking on array

function string.at(int) does the same thing, BUT checks bounds

string.length() returns the length

C++ strings

comparison operators

== compares the contents unlike C strings

<, <= , >, >= do alphanumeric comparisons

getting a C string version of a string (some functions require a C string) string.c_str()

Pointer basics

memory can be treated like a gigantic array

a pointer is like an index to that array - it just specifies the position of some data

but... some data takes up multiple spots in memory

we still have the same index to the memory array, but some things take multiple spots

Pointer basics

suppose we want to get the memory address of a variable, we use an &: cout << “Contents of addr “ << &x << “: “ << x << endl;

Note: this usage of & is different than the one we’ve seen so far

in declarations (params,defs,retvals) it signifies by reference

in other spots, it says to get the address

Pointer basics

The two meanings of *

in declaring a variable/parameter/return value: signifies that it’s a pointer variable

in other situations: dereferences the variable (gets the stuff it points to)

Midterm stuff

expected length < period length

types of questions

what does this code do?

what does this error message mean?

find the bug in this code

write some simple code (5 lines max)

write a few good test cases