C Programming - Review
C Programming - Review
BN-2N (Clara Benson Building) BN-2S (Clara Benson Building) BN-3 (Clara Benson Building)
Post-midterm Topics
Arrays Pointers Pointers and Arrays Relationship Strings Structures Dynamic Memory Allocation
What to expect?
Small questions that test specific parts
Theory; loops, conditionals, number systems, character arithmetic, arrays, strings, pointers, functions, structures, memory management, etc.
Tracing output
Can include any of the concepts
Chapter 8
Arrays
One-Dimensional Arrays
An array is a data structure containing a number of data values, all of which have the same type.
To declare an array, we must specify the type of the arrays elements and the number of elements: int a[10];
To access an array element use subscripting or indexing the array (an array of length n are indexed from 0 to n 1).
One-Dimensional Arrays
Array initialization
int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int a[10] = {1, 2, 3, 4, 5, 6}; int a[10] = {0};
Expressions of the form a[i] are lvalues, so they can be used in the same way as ordinary variables:
a[0] = 1; printf("%d\n", a[5]); ++a[i];
Multidimensional Arrays
An array may have any number of dimensions. The following declaration creates a two-dimensional array (a matrix, in mathematical terminology):
int m[5][9];
m has 5 rows and 9 columns. Both rows and columns are indexed from 0:
Multidimensional Arrays
To access the element of m in row i, column j, we must write m[i][j]. C stores arrays in row-major order, with row 0 first, then row 1, and so forth. How the m array is stored:
Multidimensional Arrays
Nested for loops are ideal for processing multidimensional arrays. Consider the problem of initializing an array for use as an identity matrix. A pair of nested for loops is perfect:
#define N 10 double ident[N][N]; int row, col; for (row = 0; row < N; row++) for (col = 0; col < N; col++) if (row == col) ident[row][col] = 1.0; else ident[row][col] = 0.0;
10 Copyright 2008 W. W. Norton & Company. All rights reserved.
Examples:
random = rand() % 100; // range 0 to 99 random = rand() % 100 + 1; // range 1 to 100 random = rand() % 30 + 1985; // range 1985-2014
11
12
Array Arguments
A function is allowed to change the elements of an array parameter, and the change is reflected in the corresponding argument. A function that modifies an array by storing zero into each of its elements:
void store_zeros(int a[], int n) { int i; for (i = 0; i < n; i++) a[i] = 0;
14 Copyright 2008 W. W. Norton & Company. All rights reserved.
Array Arguments
A call of store_zeros:
store_zeros(b, 100);
The ability to modify the elements of an array argument may seem to contradict the fact that C passes arguments by value. Chapter 12 explains why theres actually no contradiction (short answer: pass array arguments by reference).
15
Array Arguments
If a parameter is a multidimensional array, only the length of the first dimension may be omitted. If we revise sum_array so that a is a two-dimensional array, we must specify the number of columns in a:
#define LEN 10 int sum_two_dimensional_array(int a[][LEN], int n) { int i, j, sum = 0; for (i = 0; i < n; i++) for (j = 0; j < LEN; j++) sum += a[i][j]; } return sum;
16 Copyright 2008 W. W. Norton & Company. All rights reserved.
Example Programs
Programs
Reversing a Series of Numbers Checking a Number for Repeated Digits Computing Interest Dealing a Hand of Cards
17
Chapter 11
Pointers
18
Pointer Variables
If there are n bytes in memory, we can think of addresses as numbers that range from 0 to n 1:
Variables in a program occupy bytes of memory short int i;
19
Initializing pointers
int i; p = &i;
Pointer Variables
Addresses can be stored in special pointer variables. When we store the address of a variable i in the pointer variable p, we say that p points to i. A graphical representation:
21
/* prints 1 */ /* prints 1 */
/* prints 2 */ /* prints 2 */
Copyright 2008 W. W. Norton & Company. All rights reserved.
Pointer Assignment
Assume that the following declaration is in effect:
int i, j, *p, *q;
23
Pointer Assignment
If p and q both point to i, we can change i by assigning a new value to either *p or *q:
*p = 1;
*q = 2;
24
Chapter 12
25
Pointer Arithmetic
Pointers can point to array elements:
int a[10], *p; p = &a[0];
26
Pointer Arithmetic
If p points to an element of an array a, the other elements of a can be accessed by performing pointer arithmetic (or address arithmetic) on p. C supports three forms of pointer arithmetic:
Adding an integer to a pointer Subtracting an integer from a pointer Subtracting one pointer from another
27
Then:
p = &a[2];
q = p + 3;
p += 6;
28 Copyright 2008 W. W. Norton & Company. All rights reserved.
29
Example Programs
Swap the values of two integers using pointers Find largest and smallest elements in an array Sum the elements of an array using pointers Process the rows of a multidimensional array using pointers
31
Chapter 13
Strings
32
33
The compiler will automatically add a null character so that date1 can be used as a string:
34
36
39
40
Command-Line Arguments
Access to command-line arguments in main
int main(int argc, char *argv[]){}
41
The C library provides a rich set of functions for performing operations on strings.
char *strcpy(char *s1, const char *s2); size_t strlen(const char *s); char *strcat(char *s1, const char *s2); int strcmp(const char *s1, const char *s2);
42
Program Examples
Count the number of spaces in a string (using array or pointer) Checking Planet Names Writing of String functions:
Searching for the End of a String Copying a String
43
Chapter 16
Structures
44
45
47
Operations on Structures
To access a member of a structure, we use . Statements that display the values of part1:
printf("Part number: %d\n", part1.number); printf("Part name: %s\n", part1.name); printf("Quantity on hand: %d\n", part1.on_hand);
48
A call of build_part:
part1 = build_part(528, "Disk drive", 10);
50 Copyright 2008 W. W. Norton & Company. All rights reserved.
A call of print_part:
print_part(part1);
51
Program Example
Maintaining a Parts Database (Inventory)
Arrays of structures Nested structures Structures that include array members Functions that have structures as parameters Functions that return structures
52
Chapter 17
53
These functions return a value of type void * (a generic pointer). If a memory allocation function fails, it returns a null pointer.
54 Copyright 2008 W. W. Norton & Company. All rights reserved.
55
Or
a = calloc(n, sizeof(int));
56
Deallocating memory
Memory leak and garbage collector Deallocating memory using free: void free(void *ptr); The Dangling Pointer Problem
57