Title: CS3101-2 Programming Languages
1CS3101-2Programming Languages CLecture 1
- Matthew P. Johnson
- Columbia University
- Fall 2003
2Agenda
- hw0 due last night
- Arrays, mult-D arrays
- Char arrays, String objects
- Pointers References
- hw1 assigned tonight
3Array review
- Arrays are complex (non-primitive) data
structures - Ordered, fixed-length seq of vars
- All elms same type
- Access by index ar4
- Indices begin at 0
4Arrays
- Review
- Composite multiple members
- Homogeneous mems of same type
- Contiguous adjacent in memory
- Etym root is contig-, contact
- Example
- int nums3
- nums0 nums1 nums2
- nums3 ? ?
5C-style strings
- C also has
- neither a built-in string type
- Nor a string class
- Just chars
- For example
- char s3
- s0 H
- s1 i
- s2 \0
- NB \0 ! 0
- Shortcut syntax
- char s Hi //size inferred
- For non-dynamic C-style strings
- members (chars) are mutable
- chars (arrays) are immutable
6C-style strings
- So cant
- char s1100 hi
- char s2 there
- s1 s2
- s1 s2
- s3 s1 s2
- But can
- strcpy(s1, s2)
- ? s1 there
- strcat(s1, s2)
- ? s1 hi there
- strcat overwrites \0 of s1
- strcpy(s3, s1)
- ? s3 hi there
- Unsafe, though
- No space checking
- Also, can get length
- strlen(s3) 9
- And compare
- strcmp(s1, s2) lt 0 ? s1 comes before s2
- gt 0 ? s1 comes after s2
- 0 ? s1 s2
- These functions live in ltcstringgt
- NB ltlib.hgt in C ? ltclibgt in C
7C-style strings strlen
- Nothing built-in to a string/array telling its
length, like s.length() in Java - How to find out?
- For arrays in general remember/be told
- For strings look and see!
- i.e., look for \0
- for (int i 0 si ! \0 i)
-
- return i
- NB for C-style strings
- Size is fixed
- Length (distance from start to \0) varies
8Strings strlen
- char s hello
- strlen(s) 5
- s3 \0
- strlen(s)
- 2
h e l l o \0
h e \0 l o \0
9Mistakes with strings
- char s2
- s hi // error arrs are immutable
- char s2 hi //error sizeof hi 3
- char s3 hi //okay
- char t6 there
- s s t // error trying to add arrays
- strcat(s, t) // error writes too far
- strcat(s, t, sizeof(s)) // s t,h,e
- s2 \0 // okay
- strcat(s, t, sizeof(s)-1) // okay if s2\0
10C-style strings
- strcat copies t over null char of s
- strlen(strcat(s,t)) strlen(s) strlen(t)
- bytes(strcat(s,t)) strlen(s) strlen(t) 1
- bytes(s) bytes(t) -1
- Not automatically initialized (like arrays in
gen.) - char s100 ? s10 ?
- But extra space in static init set to \0
- char s100 hi ? h, i, 0, 0,
- char s100 ? 0, 0, 0,
- char s100 ? 0, 0, 0,
11String class
- Like C, Java, C has no built-in String type
- I.e., String is not part of the C language
- Like C, chars can be used as (C-style)
strings - Like Java, the std libraries have a string class
- defined in ltstringgt
- Lives in std namespace
12String class e.g.
- include ltstringgt
- include ltiostreamgt
- using namespace std
- int main()
- //String instances declared like other vars
- string school
- //Can be set to string literals
- school Columbia
- //Can be concatenated with op
- school school University
- //school University
- cout ltlt We\re at ltlt school ltlt endl
-
- ?
- g school.cpp
- a.out
- We're at Columbia University
13String operators
- NB is overloaded to apply to string objects
- is overloaded too
- school
- school University
- ? same effect
- What if school University?
- Idea 1 Precedence problem?
- A No. Assign ops have very low prec.
- Idea 2 str-lit str-lit problem?
- A Yes! is overloaded for string objects not
for string literals/chars/chars - g says
- school.cpp16 invalid operands const char2'
and const char11' to binary operator '
14String objects
- String objects ! char arrays
- chars often cannot be treated like string objs
- But string class designed so can often treat
like chars - char chars accessed quickly/unsafely with s
- char ca hi
- ca0 H
- ca-1 ? trouble
- string chars can be accessed the same way
- string school Columbia
- school0 Nl
- school-1 Y ? still unsafe!
- the operator is overloaded!
15string class
- should be sure/check that the index is valid
- assert(i gt 0 i lt 8)
- Lives in cassert
- If expr passed is false, program quits, with
message - ca.cpp13 failed assertion -1 gt 0 -1 lt 8
- Abort (core dumped)
- string chars can also be read by at member ftn
- school.at(0)
- school.at(-1) throws an exception
- does automatic bounds checking
- informs caller if index is not valid
- if main ftn ignores, we Abort
16string.length()
- Remember arrays do not know their length
- where theyre defined, length is part of type
(int10) - passed as member-type ptrs (length is lost)
- array itself is just sequence of members
- ? chars do not know theyre type
- convention last char is \0
- string objects do know theyre length
- school.length() 8
- can return part of self
- string.substr(start, length)
- school.substr(2,3) ? lum
- second param is not last char (or next-after-last
char, as in J) - its the desired length of substring
- if omitted, gives rest
17Comparing strings
- string class has a compare function similar to
strcmp - string hi "hi"
- string hi2 "hi"
- char ca "hi"
- string hi3 ca
- string there "there"
- hi.compare(hi2)
- strcmp(hi.c_str(), hi2.c_str()) 0
- hi.compare(there)
- strcmp(hi.c_str(), there.c_str() lt 0
- The big improvement, though overloaded operators
- (hi hi2) true
- (hi there) false
- (hi lt there) true
18Input with strings io2.cpp
- Can read in primitive types
- cout ltlt Enter two numbers
- //sep-ed by whitespace
- cin gtgt x gtgt y
- cout ltlt They\re product is ltlt xy ltlt endl
- String input slightly more complicated
- string s
- cout ltlt Enter some text
- //reads until whitespace
- cin gtgt s //hi there
- cout ltlt You entered ltlt s ltlt endl //hi
- String input slightly more complicated
- string s, t
- cout ltlt Enter two words
- cin gtgt s gtgt t //hi there
- cout ltlt You entered ltlt s ltlt t ltlt endl
//hithere - Alternative
- getline(cin, s)
- cout ltlt You entered ltlt s ltlt endl
19String input io3.cpp
- gtgt treats all whitespace as delimiters
- getline treats only \n as a delim (by default)
- ? combining can be problematic
- Consider
- int i
- cout ltlt "Enter i "
- cin gtgt i
- cout ltlt "i " ltlt i ltlt endl
- string s
- cout ltlt "enter s "
- getline(cin, s)
- cout ltlt "s " ltlt s ltlt endl
? g io3.cpp a.out Enter i 10 i 10 enter
s s Q Why? A cin gtgt i doesnt take \n,
so getline does
20String input io3.cpp
- Soln 1 read in the \n separately, after gtgt and
before getline - cin.ignore()
- string s
- cout ltlt "enter s "
- But only reads in fixed number of chars (1 by
default) same problem for spaces - Soln 2 call call ignore with params
- cin.ignore(1000, \n)
- string s
- cout ltlt "enter s "
- ignores up 1000 chars, up to/including \n
- Soln 3 call getline twice
- string s
- getline(cin, s)
- cout ltlt "enter s "
- getline(cin, s)
- NB for C-style strings
- Length varies
- Size varies automatically
21Converting bet. C-style, C-style
- C ? C
- Converted in assignment
- char cs hi
- string s hi
- s cs
- s hi
- C ? C
- Use strings member ftn
- string s hi
- char cs s.c_str()
- C-style strings can also be input with getline
- Should pass max num chars
- getline(cin, s, sizeof(s))
22Non-pointer data types
- Used several data types
- ints whole numbers
- floating pts decimal (non-whole) numbers
- chars letters
- Will (next time) use generic complex data types
- Structs/classes multiple-field records/objects
- In general
- a data type class of poss vals,
- corresponding to real-life concept (letter,
number, customer record) - Indy var means/refers to/denotes todays date.,
pages, etc. - But not always
23Non-pointer vars
- Vars are distinct
- Changing one does not affect another
- int x 10
- int y x
- x ? y 10
- Vars live in sep places in memory
- I.e., mappingvar-names?vars is injective,
one-to-one - Distinct var-names always refer to distinct vars
- Draw picture with x, y, array
24Pointers
- Do not correspond to real-life concept
- Indy var means another var
- Somewhat like quotes in natural language
- Snow is white, v. Snow has four letters.
- Corresponds to computer hardware concept, memory
address - Theoretically, very interesting
- ? self-reference
- ? Halting Problem interp nums as TMs
- ? Godels Incompleteness Theorem interp nums as
sentences - I am Lying, This sentence is unprovable.
- Here interp nums as var addresses
- See 3261/4236, Godel, Escher, Bach, Douglas
Hofstadter
25Pointers
- Q What is a pointer?
- A KR a var that contains the address of a
var. - For regular (first-order) vars, we have var
name, and value - char c 10
- Remember var declar/def/init does
- obtains mem for data
- attaches the name c to that mem location
- writes 10 there
26Var creation
- After declar/def/init, can use that data with the
identifier c. - When we say c, it remembers where to look
- The data lives somewhere in memory.
- Dont have to know where the data actually
isjust remember c. - Could we find out the location? (Why?)
- Turns out yes
- Ptrs are vars that take on these locations as vals
27Pointers
- p c
- op applied to a var (lvalue) evals to its
address - p is now set to the address of c
- We interp val of p as a memory location
- ? p now points to c
- applies only to lvalues
- Not consts, literals, const exprs
- Draw picture of p c in mem
28 and
- Exists inverse operator to
- the dereferencing or indirection op.
- Applied to a pointer
- evals to the pointers referent
- what it points to
- int x 1, y 2, z10 / init /
- int ip / declar /
- ip x / ip points to x /
- y ip / y set to val of ips referent,
0/ - ip 0 / ips referent, x, set to 0 /
- ip z0 / ip now points to z0
- After execution, x 0, y 1
29 and
- and (as ptr ops) are inverses
- Cancel each other out
- Examples
- x ? the value at the address of x ? x
- Who is buried in Grants tomb?
- xp ? the address of the value at the address
xp ? xp - Where is the contents of Grants tomb?
30Pointer declars
- To declar ptr-to-type var, similar to type
declar, plus - int ip
- Notice primitives, arrays, ptrs, ftns, can all
be declared together - int x, ip, y, z10, myftn()
- Interp 1 ip is an int-pointer (int) ip
- combined declar can be confusing
- int ip, x ? ip is int, x is int
- Interp 2 dereferenced ip is an int int (ip)
- more readable
- int ip, x ? ip is int, x is int
31Dereferenced ptrs
- Dereferenced ptr gives the actual lvalue, not
simply a passive value - Can be used just as the corresponding var could
be - Both accessed and modified
- ip ip 1
- , , other unaries have high precedence
- ip 1
32Uninit-ed ptrs
- Do not dereference uninit-ed ptrs
- if nec, init ptrs to NULL (0)
- int ip NULL
- int ip 0
- then check for null before deref
- if (ip ! NULL)
- if (ip)
33Dereferenced ptrs
- What about ?
- Unaries associate R-to-L
- ip ? (ip)
- But
- ip ? (ip)
- Need parenths
- ip is valid!
- pointer arithmetic
34Modifying ptrs vals
- Pointers can be modified, like other vars
- iq ip
- Example
- int ip, iq, x
- x 5
- ip x
- iq ip
- ip 10
- ? x ip iq 10
35Uses of ptrs
- Limitation mentioned before ftns have single
return value - Sometimes nice to have multiple return values
- e.g., some value, plus error/success.
- getchar() returns next char read, EOF (-1) if
error - okay, -1 isnt a char
- consider a getint() return next int read, or EOF
(-1) if error - problem EOF -1 is an int!
- Soln let return value be error/success write
next int at location of a ptr param - int getint(int )
36Passing by ref
- Remember args passed-by-value by def.
- Local var created, inited to val of arg
- Actual arg never changes
- Alternative method is pass-by-reference (not
avail in C) - Dont pass the value, pass the location of the
value - Dont send the webpage, send the URL
- Ptrs can simulate pass-by-ref
37Passing by ref swap
- Swap two ints
- Naïve swap ftn
- void swap(int x, int y)
- int temp x
- x y
- y temp
-
-
- int a 5, b 10
- swap(a,b)
- Effect nothing!
- x inited to val of a 5, y inited to val of b
10 - x and y swapped
- a and b never change
- NB couldnt be otherwise
- Consider swap(5,10)
- Cant change literals
38Passing by ref swap
- Soln 1 write a CPP macro, obviating real params
- Soln 2 Dont pass a and bs vals, but ptrs to a
and b - void swap(int px, int py)
- int temp px
- px py
- py temp
-
-
- int a 5, b 10
- swap(a,b)
- NB px, py are passed by value!
- These vals happen to be (interped as) refeences
- Draw picture!
- Soln 3 Arrays
39Pointers Arrays
- What do they have to do w/each other?
- In Java, not much
- arrays are special sorts of objs, with many mems
- arrays, like all objs, are passed by reference
- two distinct ideas
- In C/C Ptrs and arrays turn out to be almost
identical
40Pointers Arrays
- Consider int a10
- What happens?
- The id a is attached to a block of mem with room
for 10 ints - These ints accessed with a and a subscript a0,
a5 - Important the 10 ints are contiguous, in a row
- a
- 0 1 2 3 4 5 6 7 8 9
- a0 gives an int
- a0 gives the address of that int
41Pointers Arrays
- Ordinary ptrs can point to arrays
- pa a0
- ? pa
- a
- 0 1 2 3 4 5 6 7 8 9
- Now can get other array elms
- pa1 points to next
- pa2
- pa
- pa-1 points to prev
a0
42Pointers Arrays
- Q How does it know theyre ints?
- A pa is an int
- why ptrs need to know referent type
- Turns out array name evals to ptr to 0th elm
- a a0
- not applied array name
43Pointers Arrays
- Also turns out
- ai (array index notation) evals to same as
- (ai) (ptr offset notation)
- arrayindex is really short-cut notation
- arrayindex converted to (arrayindex) in
compilation - Also, arrayindex notation applicable to regular
pointers - ip20 ? (ip20) ? who knows?
- As usual, be careful!
- Q What if 2a?
- A 2a ? (2a) ? (a2) (by symmetry) ? a2
44Array/ptr example
- Consider a string-length ftn
- int strlen(char s)
- int n
- for (n 0 s ! \0 s)
- n
- return n
-
- Now change to
- int strlen(char s) Effects?
- None!
45Arrs/ptrs as ftn args
- Can call
- strlen(hi), strlen(a), strlen(pa)
- Local var s set to pt to arg passed
- Same behavior
- strlen(a2), strlen(a2)
46Passing by ref swap
- Soln 3 Pass an array containing a and b
- void swap(int AandB)
- int temp AandB0
- AandB0 AandB1
- AandB1 temp
-
-
- int AandB 5, 10
- swap(AandB)
- NB We pass one ptr (by value!), with the
understanding that a and b are contiguous - Draw picture
47Arrays v. pointers
- One important difference
- Defined-array values are immutable
- immutable cannot be changed
- cannot mutate
- Defined-array value id used to define/create
array - Illegal
- int a10
- a, a
- Restriction does not apply to
- Array members
- Array params in a ftn (formal params)
48Pointer arithmetic
- Addition/subtraction with integers
- Ptr /- n ? ptr to val distance n away
- Subtraction of two pointers
- p q ? distance bet. p and q
- p, q should point to mems of same array/data
block or NULL - No other legal ops (apart from ?)
49Pointer arithmetic e.g.
- int strlen(char s)
- char p s
- while (p ! \0)
- p
- return p-s
-
- Loops until \0
- Consider Hi H,i,\0
- Initly, p s ? H
- H ! \0 ? p / p-s 1 /
- i ! \0 ? p / p-s 2 /
- \0 \0 ? return 2
50Pointer arithmetic
- Here
- p-as-number s-as-number p-s 2
- For other elm types
- p-as-number s-as-number (p-s)sizeof(type)
- Sizeof gives in var/type
- sizeof(int), sizeof(x)
- No effect here since sizeof(char) 1
- Show sizeof.c
51String literals
- Hi ? H, i, \0
- len-n str literal is a len-n1 char
- Signal \0 indicates the end
- Same effect
- char am hi / am is immutable /
- char am H, i, \0
- Slightly different effect
- char pm hi / pm is mutable /
- pm
- am
- 0 1 2 3 4 5 6 7 8 9
am0
52Copying strings
- How to copy strings?
- s t?
- No simply makes s and t pt to same chars
- Want copy actual chars, so indy, from source t
to dest s - For now assume s pts to enough free space
- Copy strings as arrays
- void strcpy(char s, char t)
- int i 0
- while ((si ti) ! \0)
- i
-
- Each time around, 1) Assign s char to t char 2)
check for \0 - NB Assign expr evals to char assigned
53Copying strings
- Copy strings as pointers
- void strcpy(char s, char t)
- while ((s t) ! \0)
- s
- t
-
-
- Essentially the same as prev
- No index needed, but now inc both ptrs
54Copying strings
- Mix match
- void strcpy(char s, char t)
- / s arr, t ptr /
- int i 0
- while ((s ti) ! \0)
- s / s ptr /
- i / t arr /
-
-
55Copying strings
- Shorter version
- void strcpy(char s, char t)
- while ((s t) ! \0)
-
-
- Each time around s set to t, check for \0
- But unaries assoc R-to-L
- So, Q why do we get t instead of (t1)
- A in t, is eval-ed before
- But postfix lval evals to simply lval
- After eval, lval is inc-ed
- So s set to t both inc-ed
56Copying strings
- Q What is the value of the null char \0?
- A 0 any other char is ! 0
- Can take advantage of this in if
- Shortest (?) version
- void strcpy(char s, char t)
- while ((s t))
-
-
- Real strcpy lives in ltcstringgt
57Comparing strings
- Return lt0 if s lt t, 0 if st, gt0 o.w.
- int strcmp(char s, char t)
- int i
- for (i 0 si ti i)
- if (si \0)
- return 0
- return si ti
-
- NB two poss outcomes
- Get through all of s w/o discrepancy ? 0
- Find discrepancy ? amt greater si than ti
58Comparing strings
- With arrays
- int strcmp(char s, char t)
- for( s t s, t)
- if (s \0)
- return 0
- return s - t
-
- Omits i but needs , to combine inc stmts
- Very rarely used
59Pointers I/O ptrio.cpp
- A pointer is just a memory address ( number)
- We can look at the number itself
- int x 10
- int xp x
- cout ltlt "x " ltlt x ltlt endl
- cout ltlt "xp " ltlt xp ltlt endl ?
- a.out
- x 10
- xp 0xffbffa9c
- printed in hex by default
- What about char pointers?
- by default, printed as strings
- usually what we want
- to get address, reinterpret as generic pointer
- char s "hi"
- cout ltlt "s " ltlt s ltlt endl
- cout ltlt "s as int " ltlt reinterpret_castltvoidgt
(s) ltlt endl ? - s hi
- s as int 0xffbffa90
60Multi-D arrays
- 2-d array is just array of arrays
- Why we say b12 rather than b1,2
- In same way given 2-d array type ? array of 2-d
arrays 3-d array - a2634
- No fixed limit
612-D arrays - TicTacToe
- char board33
- X,X,O,
- X,O,X, ?
- ,X,X
-
- Inner braces optional
X X O
X O X
X X
b00 b01 b02
b10 b11 b12
b20 b21 b22
1000 1001 1002 1003 1004 1005 1006 1007 1008
b00 b01 b02 b10 b11 b12 b20 b21 b22
62Cmd-line args params.cpp
- Canonical main function
- int main(int argc, char argv)
- argc argument count
- argv argument vector
- exec name and params are tokenized as mems of
argv - a.out here are some params
- argv0 a.out
- argv1 here
- argv2 are
- argv3 some
- argv4 params
63Cmd-line args params2.cpp
- Many programs take params of the form
- -Pltparamgt
- -Wall
- -Dmacrodef
- Parsing params of this form quite elegant
- while (argc-- gt 0 argvargc-0 -))
- char val argvargc-2
- switch (argvargc-1)
- case D case d
- cout ltlt macro def ltlt val ltlt endl
- break
- case W case w
- cout ltlt warnings ltlt val ltlt endl
- break
- default
- print_usage()
- break
-
-
64switch statement
- Replaces many if-elses
- Similar to those in C, J
- Applies only to integral data types
- Upon entering case, falls through until break
- allows combine cases
- case D case d
- If no match, executes optional default case
65Constant ptrs, ptrs to constants
- Can make var const (immutable) with const
- const double PI 3.14
- int ftn(const int param)
- const int
- const int X 10
- int const X 10
- ptr-to-const
- const int cip x
- int const cip x
- const ptr
- int const IP x
- const ptr-to-const
- const int const CIP
- int const const CIP
66const ptr examples const.cpp
- const int x 10
- int ip x //error/cancels const
- const int cip x //val is const, ptr is not
- (cip) //not allowed
- cip //is allowed
- int y 11
- int const IP y //ptr is const, val is not
- (IP) //is allowed
- IP //not allowed
- const int const CIP x
- (CIP) //not allowed
- CIP //not allowed
67Consts arrays const2.cpp
- Array of consts
- const int nums10 1,2,3
- const int xp num2 //must be ptr to const
- xp 10 // not allowed
- Const array?
- Array is automatically const
68References
- C introduces the concept of a reference
- Often replaces need for ptrs
- Example
- int x 10
- int y x
- y ? x 11
- y is an alias for x
- y refers to x
- more precisely, x, y refer to same entity in mem
- Reference vars behave like ptrs, obj vars in J
- obj1 obj2 ? obj1, obj2 refer to same thing
69Reference params
- Reference vars mostly used for params
- Example
- void square(double num)
- num num
-
- Param is used like regular var (double)
- Doesnt req dereferencing
- But is alias to original argument
70Reference params swap
- Soln 4 for swapping references
- void swap(int x, int y)
- int temp x
- x y
- y temp
-
-
- int a 5, b 10
- swap(a,b)
- NB Only difference from original (erroneous)
swap ftn is x, y
71Benefits of references
- simpler because of no dereferencing
- safer because more limited
- references must be init-ed when defined
- int x does not compile
- 2. references cannot be null
- 3. references are immutable
- int x y
- x z resets xs value (ys value) to zs value
- x just gives address of referent ( y)
72References pointers
- Most compilers translate ref-code to ptr-code
- just as array-code is translated to ptr-code
- same underlying concept
- The safety comes from the restrictions imposed on
ref-code before translation - References used very widely in overloaded
operations - Will write some next time
73References, ptrs, and things
- int x 4 //thing
- x //address of thing
- int xp x //ptr to thing
- xp 10 //thing
- int rx x //reference to thing
- rx 10 //thing
- x 11 //same thing x rx
- int rxp rx //ptr to thing
- rxp 20 /same thing
- rxp rx x /
74Refs consts consts2.cpp
- What youd expect, similar to arrays
- Reference to const
- const int x 10
- const int rx x //must be const
- rx 10 //not allowed
- Const reference?
- References automatically const
75References objects
- In Java, refs objects tightly coupled
- obj1 obj2
- obj1.changeMe()
- ? obj2 changed too
- In C, refs objects independent
- int x 10
- int rx x
- rx
- ? rx, x both changed
- string s1 hi, s2 there
- s1 s2
- s1.append(abc)
- ? s1 changed but not s2
76Next time
- Q How can an object be assigned (by value) to
another? - A With the copy-constructor
- Topic next time Classes
- For next time
- Do reading assigned on web
- Hw1 will be up tonight
- Done individually
- Sign in before you leave!