Our reference
- Chapters 7 to 14 of An Introduction to the C Programming Language and Software Design
But let’s start with some Fall 2015 CSCI 255 notes on C vs Java.
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 assigned to pointers and pointers to pointers
- The
const
declaration modifiesconst int* pA ;
int const* pB ;
- call by reference (vs call by value)
- Write an integer swap routine
- C++ reference
strtol
man page — this is hard
- arrays are constant pointers
- the mysteries of pointer arithemetic
- passing a function with pointers
- Spring 2017 CSCI 255 notes
- Problem 15 of Spring 2017 CSCI 255 final
- Fall 2011 ECE 209 notes
Chapter 8 — Arrays and Strings
- Size of array is usually set in declaration
- until you get to Chapter 9
- What a hack to find the size —
sizeof(P)/sizeof(*P)
- Strings are null terminated
char
arrays - C string library
- arrays of pointers
- multidimensional array
Chapter 9 — Dynamic Memory
Concentrate on sections 9.1—9.3: We are skipping the CSCI 202 stuff.
- Memory regions of C
malloc
and friends- 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
struct
is a Javaclass
with onlypublic
fields — no methods - A C
union
is strange, but useful- /usr/include/netinet/ip.h vs IP4 header
- /usr/include/netinet/ip_icmp.h vs ICMP header
- Fall 2011 ECE 209 structure notes (with linked lists)
- Fall 2011 ECE 209 union examples
Chapter 12 — Bitwise Operators
We did much of this in Chapter 2 of the textbook.
- Bitwise operators: Just like 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 == c
isx & (b == c)
- Bit-fields are used in the IP and ICMP headers
Chapter 13 — Input and Output
- %[flags][width][.precision][length]specifier
- The C/C++
printf
is a bit like the Javaformat
- The C/C++
- %[*][width][length]specifier
- The C/C++
scanf
is very different from the JavaScanner
- C input uses pointers
- The C/C++
- In practice C++ I/O, with its heavy use of operator overloading and multiple inheritance, does not look at all like C and Java I/O
- File control:
fopen
andfclose
- Stream access
- Random access:
fseek
andftell
- Command line arguments
- Fall 2010 ECE 209 formatting notes
Chapter 14 — Generic Programming
This is not a strong point of C!
typedef
: frequently used by experienced programmers- macros: be very careful
union
: be very carefulvoid *
: the generic pointerqsort()
: learn how to use it