Download presentation
Presentation is loading. Please wait.
1
Chapter: 7-12 Final exam review
2
Review chapter 7-12 C pointers C characters and Strings C formatted Input/Output C Structures, Unions, Bit manipulations and Enumerations C File processing C Data structures Final exam rules, scope, and tips
3
Chapter: 7 C Pointers Review
4
Key points Pointers and pointer operators.
Pass arguments to functions by reference by using pointer. sizeof Operator and Pointer Arithmetic. The close relationships among pointers, arrays and strings. Pointers to functions.
5
Pointers and pointer operators
Contains address of a variable that has a specific value (indirect reference) Definitions: int *myPtr1, *myPtr2; Operators: Address operator: & Indirection/dereferencing operator: * * and & are inverses, They cancel each other out int y = 5; int *yPtr; yPtr = &y; /* yPtr gets address of y */ *yptr = 7; /* changes y to 7 */
6
Pass arguments to functions by reference by using pointer.
Pass address of argument using & operator Arrays are not passed with & Used as alias/nickname for variable inside of function /*Function definition*/ void double( int *number ) { *number = 2 * ( *number ); } int main (void) { int a =5; /*Function call*/ double( &a ) printf(“%d /n”, a) Const qualifiers: variable can not be changed int *const myPtr = &x; /*Constant pointer*/ const int *myPtr = &x; /*Constant int*/ const int *const Ptr = &x; /*Cons. pointer point to Cons. int*/
7
sizeof Operator and Pointer Arithmetic.
Sizeof: Returns size of operand in bytes Can be use for program that depend on data type sizes and run on different system Pointer Arithmetic: ++, --, +,+=,-,-= Operations meaningless unless performed on an array Unit: base on array type and system int v[]; vPtr2 = v[ 2 ]; vPtr = v[ 0 ]; vPtr2 – vPtr = 2 vPtr = 3000; vPtr += 2; /*vPtr = vPtr2 = 3008; 1 unit = 4 bytes*/
8
The close relationships among pointers, arrays and strings.
Arrays and pointers closely related Array name like a constant pointer Pointers can do array subscripting operations Equalities between pointer bPtr and array b[] bPtr = b; bPtr = &b[0]; bPtr[3] = b[3]; *( bPtr + 3 ) = b[3]; Arrays of pointers char *suit[ 4 ] = { "Hearts", "Diamonds", "Clubs", "Spades" };
9
Pointers to functions Function pointers Two definition ways:
Contains address of function Function pointers can be Passed to functions Stored in arrays Assigned to other function pointers Two definition ways: int *compare( int a, int b ) Rerturn a pointer to int int ( *compare )( int a, int b ) Return an int
10
C Characters and Strings Review
Chapter 8 C Characters and Strings Review
11
Key points Fundamental of Stings and Character.
Character-handling library (ctype). String-conversion functions (stdlib). Standard Input/Output library functions(stdio). String-processing functions of the String-Handling Library (string). Memory functions of the the String-Handling Library.
12
Fundamentals of Strings and Characters
Building blocks of programs Every program is a sequence of meaningfully grouped characters Character constant An int value represented as a character in single quotes 'z' represents the integer value of z Strings Strings are arrays of characters String is a pointer to first character Value of string is the address of first character String definitions char color[] = "blue"; char *colorPtr = "blue"; Inputting strings scanf("%s", word); Remember that strings represented as character arrays end with '\0‘
13
Character Handling Library
#include <ctype.h>
14
String-Conversion Functions
#include <stdlib.h>
15
Standard Input/Output Library Functions
#include <stdio.h>
16
String Manipulation Functions of the String Handling Library
#include <string.h>
17
Comparison Functions of the String-Handling Library
18
Search functions of the String-Handling Library
19
Memory Functions of the String-Handling Library
#include <stdlib.h>
20
Other functions of the string-handling library
21
C Formatted Input / Output Review
Chapter 9 C Formatted Input / Output Review
22
Key points Input and output streams. Format output using printf.
Format input using scanf.
23
Input and output streams.
Sequences of characters organized into lines Performs all input and output Can often be redirected Input: printf, sprint, puts, putchar Output: scanf, sscanf, gets, getchar
24
Format output using printf
printf( format-control-string, other-arguments ); printf( "\t%.3f\n\t%.3e\n\t%.3g\n\n", f, f, f ); Conversion specifiers Integer: d, i, o, u, x, X, h, l Floating point: f, e, E, g, G, L Character and String: c, s Others: p, n, % Print with field widths and precision Field widths: Size of field in which data is printed Syntax: printf( "%4d\n", -12 ); Precision: Integer, Floating point, String Syntax: printf( "\t%.4d\n\t%.9d\n\n", i, i ); Flags: -, +, space, #, 0 Escape sequence: \
25
Format input using scanf
Scanf(format-control-string, other-arguments) scanf( "%d%i%i%i%o", &a, &b, &c, &d, &e,); Format control string: Contain conversion specifiers only Conversion specifiers: Integers, Floating point, Character and String Scan set [scan characters]
26
C Structures, Unions, Bit Manipulations and Enumerations Review
Chapter 10 C Structures, Unions, Bit Manipulations and Enumerations Review
27
Key points Understanding & using Bitwise operators. Structures Unions
Enumerations. Bitwise operators.
28
Structure Structure: Definition: 2 ways Accessing structure members
collection of related variables struct card { char *face; char *suit; }; Definition: 2 ways card oneCard, deck[ 52 ], *cPtr; Or: char *suit; } oneCard, deck[ 52 ], *cPtr; Accessing structure members (.) used with structure variables (->) used with pointers to structure variables typedef: create shorter type names typedef struct Card *CardPtr;
29
Unions Union: like structure but union definitions
Only contains one data member at a time Members of a union share space Only the last data member defined can be accessed union definitions Same as struct union Number { int x; float y; }; union Number value;
30
Enumeration Enumeration
Set of integer constants represented by identifiers Values are automatically set Example: enum Months { JAN = 1, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC}; Creates a new type enum Months in which the identifiers are set to the integers 1 to 12 Enumeration variables can only assume their enumeration constant values (not the integer representations)
31
10.9 Bitwise Operators
32
Bit Fields Bit field : Enable better memory utilization
struct BitCard { unsigned face : 4; unsigned suit : 2; unsigned color : 1; }; Must be defined as int or unsigned Cannot access individual bits Member of a structure whose size (in bits) has been specified
33
C File Processing Review
Chapter 11 C File Processing Review
34
Key points Data hierarchy File & Stream
Create, read, write and update: Sequential access file Random access file
35
Data hierarchy Bit, bytes, characters, records, files database
36
File & Stream File: Stream: Read/Write function
A sequence of bytes, ends with end-of-file marker Structure File descriptor File control block Stream: Provide communication channel between files and programs File pointer: to be returned when open a file stdin, stdout, stderr Read/Write function fgets, fputc, fgets, fputs, fscanf, fprintf
37
Create, read, write and update
FILE *cfPtr; cfPtr = fopen(“clients.dat", “w”); Opening modes Character mode: r,w,a,r+,w+,a+, Binary mode: rb,wb,ab,rb+,wb+,ab+ Functions Random Sequential Read fread fscanf Write fwrite fprintf Pointer position fseek rewind Others feof, fclose
38
Sequential vs. Random access file
Properties Random Sequential Length of records Fixed Dynamic Access individual records without searching through other records Yes No, should be searched through other records Instant access to records No Data can be inserted without destroying other data No, Other data can be destroyed Data previously stored can be updated or deleted without overwriting Storage memory High Low Human readable
39
C Data Structures Review
Chapter 12 C Data Structures Review
40
Key points Self-Referential Structure Dynamic Memory Allocation
Dynamic data structures Linked lists Stacks Queues Trees
41
Self-Referential Structures
Structure contains a pointer to a structure of the same type struct node { int data; struct node *nextPtr; } nextPtr Points to an object of type node Referred to as a link
42
Dynamic Memory Allocation
malloc (): Obtain memory for a new structure Takes number of bytes to allocate Return a void * pointer or Null Example: newPtr = malloc( sizeof( struct node ) ); free (): Release memory Deallocates memory allocated by malloc Takes a pointer as an argument free ( newPtr );
43
Dynamic data structures
Data structures that grow and shrink during execution Linked lists Allow insertions and removals anywhere Can be used when the number of data element is unpredictable Stacks Allow insertions and removals only at top of stack Rule: Last in first out Queues Allow insertions at the back and removals from the front Rule: First in, first out Binary trees High-speed searching and sorting of data and efficient elimination of duplicate data items
44
Final exam rules, scope, & tips
You are not allowed to refer to any materials except one cheating paper. The examination starts at 9:00 AM and ends at 11:00AM sharp. You can leave any time if you want before 11:00AM. Scope: Chapter 7-12 Tips: Review all lecture slides Review all class assignments and home works Do all Self-Review exercises after each chapter in text book Practice as many as possible
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.