... 1/16 CSCI 2212: Intermediate Programming / C Review, Chapters 10 and 11 Alice E. Fischer February 3, 2016
... 2/16 Outline Basic Types and Diagrams
... 3/16 Basic Types and Diagrams Types in C C has many built-in types. These include: int and unsigned int short and unsigned short long and unsigned long char, signed char, and unsigned char bool (since C-99) float, double, and long double Chapter 7 lists the range of values that can be stored in each type and gives the limits of precision for the floating types.
... 4/16 Basic Types and Diagrams Pictures of the Basic Types. In the diagrams, the size of the box is proportional to the number of bytes needed to store a value. char or bool short int, float, or pointer double an array of char
... 5/16 Diagrams of pointers Pointer operations
... 6/16. int k = 3; int* p; int* q = NULL; int* pi = &k; A pointer, uninitialized p? A NULL pointer A pointer to an integer After executing p = pi; q pi p 0 (memory location 0) 3 k
... 7/16 Pointer Details A pointer is an address. We say it has star-level 1. An int has star-level 0. The address of an int has level 1. A pointer variable is a variable that stores an address. are used to process arrays. An array has star-level 1 are used to return answers from functions. int k = 5; int ary[4] = {}; // An array: 4 slots initialized to 0 int* p1 = &k; // Star-level must match. int* p2 = arr; int* p3 = &arr[0]; // Same as line above. int* q = nullptr; // In C, use NULL.
... 8/16 and Arrays. int k = 5; int ary[4] = {}; int* p1 = &k; int* p2= arr; int* q = nullptr k 5 arr [0] [1] [2] [[3] 0 0 0 0 p1 p2 q
... 9/16 Pointer Semantics The following things make sense ONLY if pointers p and q are pointing into the same array. Pointer arithmetic works in units of array slots, not bytes. Point to the first byte that is NOT in the array. Add int N to p to move p N slots toward end of the array. Subtract q-p to find the # of array slots between p and q Compare p==q to find if they point to the same array slot Compare p > q to learn which one is at a larger subscript
... 10/16 Pointer Syntax The name of an array is translated as a pointer to its slot 0. The slot 0 of an array is guaranteed to be at a lower address than slot 1. In a declaration, the * is part of the type, not part of the variable name. Avoid declaring more than one pointer in a declaration, since the * must be repeated for each, and that is too easy to forget. In the code, *p means the contents of the variable p points at. In the code, p means the address that p points at.
... 11/16 Using *. int k = 7; int arr[3]; int* p = arr; int* q = &k; q p 7096 8000 7096 k 7 8000 8004 8008??? arr *p = *q; p = q; q p 7096 7096 7096 7 8000 8004 8008 7?? If you write p =, you will change the contents of p. If you write *p = you will change the value of whatever p points at. The star level on both sides of the = must be the same.
... 12/16 Usage: array processing. are often used to process arrays. Here is how: Establish a single name that represents the beginning of the array, either the name you gave it when you declared it, or the name of the pointer in which you store the result of a new dynamic allocation. This is the name to use when you free the dynamic memory. Create an end pointer to point at the first address that is not in the array. Use this to end loops. Create a scanning pointer that starts at the head and goes to the tail. p1 = arr; // Point at head of the array. p2 = arr+4; // Point at the first non-array slot. q = p1; // Start a scanning pointer at the head. for (q=p1; q<p2; ++q)... // process the whole array.
... 13/16 Head and Tail, Cursors. p1 = arr; // This is a stationary head pointer. p2 = arr+4; // This is an offboard tail pointer. q = p1; // This is a scanning pointer. for (q=p1; q<p2; ++q)... p1 arr [0] [1] [2] [3] 0 0 0 0? q p2
... 14/16 Pointer arithmetic, L- and R-values. Some error comments and most professional documentation talks about L-values and R-values. int k = 7; int* q = &k; q 7096 7096 7 k The L-value of k is 7096. Its R-value is 7. The R-value of q is 7096. *q is the same as k. int arr[3] = {2, 3, 5} int* p = arr; int* end = arr + 3; p 8000 end 8012 arr 8000 8004 8008 2 3 5 8000 8004 8008 After ++p p 8004 11 3 5
... 15/16 Array Parameters An array argument is ALWAYS passed by address. This saves time and space. In the function call, use the array name, without any & or subscript. In the function, declare the parameter to be a pointer or an array; it makes little difference. Inside the function, you can use subscripts, even if the parameter is a * type. The length of the array is NOT passed to the function as part of the array, so it must be passed separately. If you declare an array parameter with a length, the length will be ignored.
... 16/16 Pointer Parameters A pointer parameter can be used to return an answer from a function. For example, to return a double, you might declare the parameter to be double* db. To call the function, you would provide an array argument or the address of a non-array. Inside the code, near the end, you would write *db = answer; This will deliver the answer back to the caller s variable.