Our reference
- Chapters 7 to 12 of An Introduction to the C Programming Language and Software Design
- a pretty good reference from Univerity of Illinois at Urbana-Champaign
A review
- Fall 2015 CSCI 255 notes on C vs Java.
int main(int argc, char *argv[]) { }-
System.outvsstdoutvsstd::coutwith<<andstd::iomainip -
System.invsstdinvsstd::cinwith>>andstd::iomainip - C is not object oriented
- C does not have
try/catch— You must test return values!
Chapter 7 — Pointers
- Draw elements of the following types
int *char *int **void *
- Two new operators
- unary
&— address-of - unary
*— dererence - study examples on page 50
- unary
- Examples of assigning to pointers and pointing to pointers
- The
constdeclaration modifiersconst int* pA ;int const* pB ;
- call by reference (vs call by value)
- Let’s write an integer swap routine
- C++ reference variables
strtolman page — look at the example!
NULLin C is similar tonullin Java.- passing a function with pointers
- Scary examples in Fall 2011 ECE 209 notes
Chapter 8 — Arrays and Strings
- Size of array is usually set in declaration
- until you get to Chapter 9
- If
Pis an array of integers, thenPis a constant pointer to an integer. - Pointer arithmetic — The following rules formally apply only
when the pointers address lvalues within the same array.
P[i]is*(P+i).- If
Pis a pointer to an integer, thenP+iis a pointer to thei’th integer after the integer referenced byP. - If
Pis a pointer to an integer, thenP-iis a pointer to thei’th integer before the integer referenced byP. P++incrementsPto point to the next integer in the array.P--decrementsPto point to the previous integer in the array.- An odd collection:
P--,P++,P < Q,P == Q,P <= Q,P >= Q,P += 5,P - Q. - The address of the non-existent element one past the end of the array can be used in comparisons. This is weird stuff.
- A hack to find the number of elements —
sizeof(P)/sizeof(*P). Only works for static arrays. - Strings are null terminated
chararrays - C string library
- arrays of pointers vs multidimensional array
- Problem 16 of Fall 2016 CSCI 255 final
Chapter 9 — Dynamic Memory
Concentrate on sections 9.1—9.3: We are skipping the CSCI 202 stuff.
- Memory regions of C
-
mallocand friends- in C
int *X ; X = (int *) malloc(n * sizeof(int)) ; if (X == NULL) … - in C++
int *X ; X = new int[n] ; if (X == NULL) …
- in C
-
There is no garbage collection in C — use
free
Chapter 10 — The C Preprocessor
- Fall 2009 ECE 209 C Preprocessor notes
- Try: gcc -E pointers.c > pointers.c-after-cpp
Chapter 11 — Structures and Unions
- A C
structis a Javaclasswith onlypublicfields — no methods - A C
unionis strange, but useful - Fall 2011 ECE 209 structure notes (with linked lists). We used to do this in CSCI 202. See Problem 4 in Spring 1994 CSCI 202 final.
- Fall 2011 ECE 209 union examples
Chapter 12 — Bitwise Operators
We do this in Chapter 2 of the textbook.
- Bitwise operators: Just like in Java except there is no
>>>- C’s
signed >>is like Java’s>> - C’s
unssigned >>is like Java’s>>>
- C’s
- Bitwise logical operators have a lower precedence than relational
operators:
x & b == cisx & (b == c) - Bit-fields are used in the IP and ICMP headers