6.087 Lecture 5 – January 15, 2010 Review Pointers and Memory Addresses Physical and Virtual Memory Addressing and Indirection Functions with Multiple Outputs Arrays and Pointer Arithmetic Strings String Utility Functions Searching and Sorting Algorithms Linear Search A Simple Sort Faster Sorting Binary Search 1
Review: Unconditional jumps • goto keyword: jump somewhere else in the same function • Position identified using labels • Example (for loop) using goto: { int i = 0 , n = 20; /∗ i n i t i a l i z a t i o n ∗/ goto loop_cond ; loop_body : /∗ body of loop here ∗/ i ++; loop_cond : i f ( i < n ) /∗ loop condition ∗/ goto loop_body ; } • Excessive use of goto results in “spaghetti” code 1
Review: I/O Functions • I/O provided by stdio.h, not language itself • Character I/O: putchar(), getchar(), getc(), putc(), etc. • String I/O: puts(), gets(), fgets(), fputs(), etc. • Formatted I/O: fprintf(), fscanf(), etc. • Open and close files: fopen(), fclose() • File read/write position: feof(), fseek(), ftell(), etc. . . . • 2
Review: printf() and scanf() • Formatted output: int printf (char format[], arg1, arg2, ...) • Takes variable number of arguments • Format specification: %[flags][width][.precision][length]<type> • types: d, i (int), u, o, x, X (unsigned int), e, E, f, F, g, G (double), c (char), s (string) • flags, width, precision, length - modify meaning and number of characters printed • Formatted input: scanf() - similar form, takes pointers to arguments (except strings), ignores whitespace in input 3
Review: Strings and character arrays • Strings represented in C as an array of characters (char []) • String must be null-terminated (’0’ at end) Declaration: • char str [] = "I am a string."; or char str[20] = "I am a string."; • strcpy() - function for copying one string to another • More about strings and string functions today. . . 4
6.087 Lecture 5 – January 15, 2010 Review Pointers and Memory Addresses Physical and Virtual Memory Addressing and Indirection Functions with Multiple Outputs Arrays and Pointer Arithmetic Strings String Utility Functions Searching and Sorting Algorithms Linear Search A Simple Sort Faster Sorting Binary Search 5
Pointers and addresses • Pointer: memory address of a variable • Address can be used to access/modify a variable from anywhere • Extremely useful, especially for data structures • Well known for obfuscating code 5
Physical and virtual memory • Physical memory: physical resources where data can be stored and accessed by your computer cache • RAM • hard disk • • removable storage • Virtual memory: abstraction by OS, addressable space accessible by your code 6
Physical memory considerations • Different sizes and access speeds • Memory management – major function of OS • Optimization – to ensure your code makes the best use of physical memory available • OS moves around data in physical memory during execution • Embedded processors – may be very limited 7
Virtual memory • How much physical memory do I have? Answer: 2 MB (cache) + 2 GB (RAM) + 100 GB (hard drive) + . . . • How much virtual memory do I have? Answer: <4 GB (32-bit OS), typically 2 GB for Windows, 3-4 GB for linux • Virtual memory maps to different parts of physical memory • Usable parts of virtual memory: stack and heap • stack: where declared variables go • heap: where dynamic memory goes 8
Addressing variables • Every variable residing in memory has an address! What doesn’t have an address? • • register variables • constants/literals/preprocessor defines • expressions (unless result is a variable) • How to find an address of a variable? The & operator int n = 4; double pi = 3.14159; int ∗pn = &n ; /∗ address of integer n ∗/ double ∗ ppi = &pi ; /∗ address of double pi ∗/ • Address of a variable of type t has type t * 9
Dereferencing pointers • I have a pointer – now what? • Accessing/modifying addressed variable: dereferencing/indirection operator * /∗ p r i n t s " pi = 3.14159n" ∗/ p r i n t f ("pi = %gn" ,∗ ppi ) ; /∗ pi now equals 7.14159 ∗/ ∗ ppi = ∗ ppi + ∗pn ; • Dereferenced pointer like any other variable • null pointer, i.e. 0 (NULL): pointer that does not reference anything 10
Casting pointers • Can explicitly cast any pointer type to any other pointer type ppi = (double ∗)pn; /∗ pn originally of type ( int ∗) ∗/ • Implicit cast to/from void * also possible (more next week. . . ) • Dereferenced pointer has new type, regardless of real type of data • Possible to cause segmentation faults, other difficult-to-identify errors • What happens if we dereference ppi now? 11
Functions with multiple outputs • Consider the Extended Euclidean algorithm ext_euclid(a,b) function from Wednesday’s lecture • Returns gcd(a, b), x and y s.t. ax + by = gcd(a, b) • Used global variables for x and y • Can use pointers to pass back multiple outputs: int ext_euclid(int a, int b, int ∗x, int ∗y); • Calling ext_euclid(), pass pointers to variables to receive x and y: int x , y , g ; /∗ assume a , b declared previously ∗/ g = ext_euclid (a , b,&x ,&y ) ; • Warning about x and y being used before initialized 12
Accessing caller’s variables • Want to write function to swap two integers • Need to modify variables in caller to swap them • Pointers to variables as arguments void swap( int ∗x , int ∗y ) { int temp = ∗x ; ∗x = ∗y ; ∗y = temp ; } • Calling swap() function: int a = 5 , b = 7; swap(&a , &b ) ; /∗ now, a = 7 , b = 5 ∗/ 13
Variables passing out of scope • What is wrong with this code? #include < stdio . h> char get_message ( ) { ∗ char msg [ ] = "Aren’t pointers fun?" ; return msg; } int main ( void ) { char s t r i n g = get_message ( ) ; ∗ puts ( s t r i n g ) ; return 0; } 14
Variables passing out of scope • What is wrong with this code? #include < stdio . h> char get_message ( ) { ∗ char msg [ ] = "Aren’t pointers fun?" ; return msg; } int main ( void ) { char s t r i n g = get_message ( ) ; ∗ puts ( s t r i n g ) ; return 0; } • Pointer invalid after variable passes out of scope 14
6.087 Lecture 5 – January 15, 2010 Review Pointers and Memory Addresses Physical and Virtual Memory Addressing and Indirection Functions with Multiple Outputs Arrays and Pointer Arithmetic Strings String Utility Functions Searching and Sorting Algorithms Linear Search A Simple Sort Faster Sorting Binary Search 15
Arrays and pointers • Primitive arrays implemented in C using pointer to block of contiguous memory • Consider array of 8 ints: int arr [8]; • Accessing arr using array entry operator: int a = arr [0]; • arr is like a pointer to element 0 of the array: int ∗pa = arr; int ∗pa = &arr[0]; ⇔ • Not modifiable/reassignable like a pointer 15
The sizeof() operator • For primitive types/variables, size of type in bytes: int s = sizeof(char); /∗ == 1 ∗/ double f; /∗ sizeof(f) == 8 ∗/ (64-bit OS) • For primitive arrays, size of array in bytes: int arr [8]; /∗ sizeof(arr) == 32 ∗/ (64-bit OS) long arr [5]; /∗ sizeof(arr) == 40 ∗/ (64-bit OS) • Array length: /∗ needs to be on one l i n e when implemented ∗/ #define array_length ( arr ) ( sizeof ( arr ) == 0 ? 0 : sizeof ( arr ) / sizeof ( ( arr ) [ 0 ] ) ) • More about sizeof() next week. . . 16
Pointer arithmetic • Suppose int ∗pa = arr; • Pointer not an int, but can add or subtract an int from a pointer: pa + i points to arr[i] • Address value increments by i times size of data type Suppose arr[0] has address 100. Then arr[3] has address 112. • Suppose char ∗ pc = (char ∗)pa; What value of i satisfies (int ∗)(pc+i) == pa + 3? 17
Pointer arithmetic • Suppose int ∗pa = arr; • Pointer not an int, but can add or subtract an int from a pointer: pa + i points to arr[i] • Address value increments by i times size of data type Suppose arr[0] has address 100. Then arr[3] has address 112. • Suppose char ∗ pc = (char ∗)pa; What value of i satisfies (int ∗)(pc+i) == pa + 3? i = 12 • 17
6.087 Lecture 5 – January 15, 2010 Review Pointers and Memory Addresses Physical and Virtual Memory Addressing and Indirection Functions with Multiple Outputs Arrays and Pointer Arithmetic Strings String Utility Functions Searching and Sorting Algorithms Linear Search A Simple Sort Faster Sorting Binary Search 18
Strings as arrays • Strings stored as null-terminated character arrays (last character == ’0’) • Suppose char str [] = "This is a string."; and char ∗ pc = str ; • Manipulate string as you would an array ∗(pc+10) = ’S’; puts(str ); /∗ prints "This is a String ." ∗/ 18
String utility functions • String functions in standard header string.h • Copy functions: strcpy(), strncpy() char ∗ strcpy( strto ,strfrom ); – copy strfrom to strto char ∗ strncpy(strto ,strfrom,n); – copy n chars from strfrom to strto • Comparison functions: strcmp(), strncmp() int strcmp(str1,str2 ); – compare str1, str2; return 0 if equal, positive if str1>str2, negative if str1<str2 int strncmp(str1,str2,n); – compare first n chars of str1 and str2 • String length: strlen() int strlen ( str ); – get length of str 19
More string utility functions • Concatenation functions: strcat(), strncat() char ∗ strcat ( strto ,strfrom ); – add strfrom to end of strto char ∗ strncat( strto ,strfrom,n); – add n chars from strfrom to end of strto • Search functions: strchr(), strrchr() char ∗ strchr( str ,c); – find char c in str, return pointer to first occurrence, or NULL if not found char ∗ strrchr ( str ,c); – find char c in str, return pointer to last occurrence, or NULL if not found • Many other utility functions exist. . . 20
6.087 Lecture 5 – January 15, 2010 Review Pointers and Memory Addresses Physical and Virtual Memory Addressing and Indirection Functions with Multiple Outputs Arrays and Pointer Arithmetic Strings String Utility Functions Searching and Sorting Algorithms Linear Search A Simple Sort Faster Sorting Binary Search 21
Searching and sorting • Basic algorithms • Can make good use of pointers • Just a few examples; not a course in algorithms • Big-O notation 21
Searching an array • Suppose we have an array of int’s int arr [100]; /∗ array to search ∗/ • Let’s write a simple search function: int linear_search ( int val ) { ∗ int parr , parrend = arr + array_length ( arr ) ; ∗ ∗ for ( parr = arr ; parr < parrend ; parr ++) { i f (∗ parr == val ) return parr ; } return NULL; } 22
A simple sort • A simple insertion sort: O(n2) • iterate through array until an out-of-order element found insert out-of-order element into correct location • • repeat until end of array reached • Split into two functions for ease-of-use int arr [ 1 0 0 ] ; /∗ array to sort ∗/ void shift_element ( unsigned int i ) { /∗ do i n s e r t i o n of out−of−order element ∗/ } void i n s e r t i o n _ s o r t ( ) { /∗ main i n s e r t i o n sort loop ∗/ /∗ c a l l shift_element ( ) f o r each out−of−order element ∗/ } 23
Shifting out-of-order elements • Code for shifting the element /∗ move previous elements down u n t i l i n s e r t i o n point reached ∗/ void shift_element ( unsigned int i ) { int ivalue ; /∗ guard against going outside array ∗/ for ( ivalue = arr [ i ] ; i && arr [ i −1] > ivalue ; i −−) arr [ i ] = arr [ i −1]; /∗ move element down ∗/ arr [ i ] = ivalue ; /∗ i n s e r t element ∗/ } 24
Insertion sort • Main insertion sort loop /∗ i t e r a t e u n t i l out−of−order element found ; s h i f t the element , and continue i t e r a t i n g ∗/ void i n s e r t i o n _ s o r t ( void ) { unsigned int i , len = array_length ( arr ) ; for ( i = 1; i < len ; i ++) i f ( arr [ i ] < arr [ i −1]) shift_element ( i ) ; } • Can you rewrite using pointer arithmetic instead of indexing? 25
Quicksort • Many faster sorts available (shellsort, mergesort, quicksort, . . . ) • Quicksort: O(n log n) average; O(n2) worst case • choose a pivot element • move all elements less than pivot to one side, all elements greater than pivot to other • sort sides individually (recursive algorithm) • Implemented in C standard library as qsort() in stdlib.h 26
Quicksort implementation • Select the pivot; separate the sides: void quick_sort ( unsigned int l e f t , unsigned int r i g h t ) { unsigned int i , mid ; int pivot ; i f ( l e f t >= r i g h t ) return ; /∗ nothing to sort ∗/ /∗ pivot i s midpoint ; move to l e f t side ∗/ swap( arr + l e f t , arr + ( l e f t + r i g h t ) / 2 ) ; pivot = arr [ mid = l e f t ] ; /∗ separate i n t o side < pivot ( l e f t +1 to mid ) and side >= pivot ( mid+1 to r i g h t ) ∗/ for ( i = l e f t +1; i <= r i g h t ; i ++) i f ( arr [ i ] < pivot ) swap( arr + ++mid , arr + i ) ; [Kernighan and Ritchie. The C Programming Language. 2nd ed. Prentice Hall, 1988.] © Prentice Hall. All rights reserved. This content is excluded from our Creative Commons license. For more information, see http://ocw.mit.edu/fairuse. 27
Quicksort implementation • Restore the pivot; sort the sides separately: /∗ restore pivot p os it io n ∗/ swap( arr + l e f t , arr +mid ) ; /∗ sort two sides ∗/ i f ( mid > l e f t ) quick_sort ( l e f t , mid −1); i f ( mid < r i g h t ) quick_sort ( mid+1 , r i g h t ) ; } • Starting the recursion: quick_sort(0, array_length(arr) − 1); [Kernighan and Ritchie. The C Programming Language. 2nd ed. Prentice Hall, 1988.] © Prentice Hall. All rights reserved. This content is excluded from our Creative Commons license. For more information, see http://ocw.mit.edu/fairuse. 28
Discussion of quicksort • Not stable (equal-valued elements can get switched) in present form • Can sort in-place – especially desirable for low-memory environments • Choice of pivot influences performance; can use random pivot • Divide and conquer algorithm; easily parallelizeable • Recursive; in worst case, can cause stack overflow on large array 29
Searching a sorted array • Searching an arbitrary list requires visiting half the elements on average • Suppose list is sorted; can make use of sorting information: • if desired value greater than value and current index, only need to search after index • each comparison can split list into two pieces • solution: compare against middle of current piece; then new piece guaranteed to be half the size • divide and conquer! • More searching next week. . . 30
Binary search • Binary search: O(log n) average, worst case: int binary_search ( int val ) { ∗ unsigned int L = 0 , R = array_length ( arr ) , M; while ( L < R) { M = ( L+R−1)/2; i f ( val == arr [M] ) return arr +M; /∗ found ∗/ else i f ( val < arr [M] ) R = M; /∗ in f i r s t h a l f ∗/ else L = M+1; /∗ in second h a l f ∗/ } return NULL; /∗ not found ∗/ } 31
Binary search • Worst case: logarithmic time • Requires random access to array memory • on sequential data, like hard drive, can be slow • seeking back and forth in sequential memory is wasteful • better off doing linear search in some cases • Implemented in C standard library as bsearch() in stdlib.h 32
Summary Topics covered: • Pointers: addresses to memory • physical and virtual memory • arrays and strings • pointer arithmetic • Algorithms • searching: linear, binary • sorting: insertion, quick 33
MIT OpenCourseWare http://ocw.mit.edu 6.087 Practical Programming in C January (IAP) 2010 For information about citing these materials or our Terms of Use,visit: http://ocw.mit.edu/terms.

Learn C program in Complete c programing string and its functions like array .pdf

  • 1.
    6.087 Lecture 5– January 15, 2010 Review Pointers and Memory Addresses Physical and Virtual Memory Addressing and Indirection Functions with Multiple Outputs Arrays and Pointer Arithmetic Strings String Utility Functions Searching and Sorting Algorithms Linear Search A Simple Sort Faster Sorting Binary Search 1
  • 2.
    Review: Unconditional jumps •goto keyword: jump somewhere else in the same function • Position identified using labels • Example (for loop) using goto: { int i = 0 , n = 20; /∗ i n i t i a l i z a t i o n ∗/ goto loop_cond ; loop_body : /∗ body of loop here ∗/ i ++; loop_cond : i f ( i < n ) /∗ loop condition ∗/ goto loop_body ; } • Excessive use of goto results in “spaghetti” code 1
  • 3.
    Review: I/O Functions •I/O provided by stdio.h, not language itself • Character I/O: putchar(), getchar(), getc(), putc(), etc. • String I/O: puts(), gets(), fgets(), fputs(), etc. • Formatted I/O: fprintf(), fscanf(), etc. • Open and close files: fopen(), fclose() • File read/write position: feof(), fseek(), ftell(), etc. . . . • 2
  • 4.
    Review: printf() andscanf() • Formatted output: int printf (char format[], arg1, arg2, ...) • Takes variable number of arguments • Format specification: %[flags][width][.precision][length]<type> • types: d, i (int), u, o, x, X (unsigned int), e, E, f, F, g, G (double), c (char), s (string) • flags, width, precision, length - modify meaning and number of characters printed • Formatted input: scanf() - similar form, takes pointers to arguments (except strings), ignores whitespace in input 3
  • 5.
    Review: Strings andcharacter arrays • Strings represented in C as an array of characters (char []) • String must be null-terminated (’0’ at end) Declaration: • char str [] = "I am a string."; or char str[20] = "I am a string."; • strcpy() - function for copying one string to another • More about strings and string functions today. . . 4
  • 6.
    6.087 Lecture 5– January 15, 2010 Review Pointers and Memory Addresses Physical and Virtual Memory Addressing and Indirection Functions with Multiple Outputs Arrays and Pointer Arithmetic Strings String Utility Functions Searching and Sorting Algorithms Linear Search A Simple Sort Faster Sorting Binary Search 5
  • 7.
    Pointers and addresses •Pointer: memory address of a variable • Address can be used to access/modify a variable from anywhere • Extremely useful, especially for data structures • Well known for obfuscating code 5
  • 8.
    Physical and virtualmemory • Physical memory: physical resources where data can be stored and accessed by your computer cache • RAM • hard disk • • removable storage • Virtual memory: abstraction by OS, addressable space accessible by your code 6
  • 9.
    Physical memory considerations •Different sizes and access speeds • Memory management – major function of OS • Optimization – to ensure your code makes the best use of physical memory available • OS moves around data in physical memory during execution • Embedded processors – may be very limited 7
  • 10.
    Virtual memory • Howmuch physical memory do I have? Answer: 2 MB (cache) + 2 GB (RAM) + 100 GB (hard drive) + . . . • How much virtual memory do I have? Answer: <4 GB (32-bit OS), typically 2 GB for Windows, 3-4 GB for linux • Virtual memory maps to different parts of physical memory • Usable parts of virtual memory: stack and heap • stack: where declared variables go • heap: where dynamic memory goes 8
  • 11.
    Addressing variables • Everyvariable residing in memory has an address! What doesn’t have an address? • • register variables • constants/literals/preprocessor defines • expressions (unless result is a variable) • How to find an address of a variable? The & operator int n = 4; double pi = 3.14159; int ∗pn = &n ; /∗ address of integer n ∗/ double ∗ ppi = &pi ; /∗ address of double pi ∗/ • Address of a variable of type t has type t * 9
  • 12.
    Dereferencing pointers • Ihave a pointer – now what? • Accessing/modifying addressed variable: dereferencing/indirection operator * /∗ p r i n t s " pi = 3.14159n" ∗/ p r i n t f ("pi = %gn" ,∗ ppi ) ; /∗ pi now equals 7.14159 ∗/ ∗ ppi = ∗ ppi + ∗pn ; • Dereferenced pointer like any other variable • null pointer, i.e. 0 (NULL): pointer that does not reference anything 10
  • 13.
    Casting pointers • Canexplicitly cast any pointer type to any other pointer type ppi = (double ∗)pn; /∗ pn originally of type ( int ∗) ∗/ • Implicit cast to/from void * also possible (more next week. . . ) • Dereferenced pointer has new type, regardless of real type of data • Possible to cause segmentation faults, other difficult-to-identify errors • What happens if we dereference ppi now? 11
  • 14.
    Functions with multipleoutputs • Consider the Extended Euclidean algorithm ext_euclid(a,b) function from Wednesday’s lecture • Returns gcd(a, b), x and y s.t. ax + by = gcd(a, b) • Used global variables for x and y • Can use pointers to pass back multiple outputs: int ext_euclid(int a, int b, int ∗x, int ∗y); • Calling ext_euclid(), pass pointers to variables to receive x and y: int x , y , g ; /∗ assume a , b declared previously ∗/ g = ext_euclid (a , b,&x ,&y ) ; • Warning about x and y being used before initialized 12
  • 15.
    Accessing caller’s variables •Want to write function to swap two integers • Need to modify variables in caller to swap them • Pointers to variables as arguments void swap( int ∗x , int ∗y ) { int temp = ∗x ; ∗x = ∗y ; ∗y = temp ; } • Calling swap() function: int a = 5 , b = 7; swap(&a , &b ) ; /∗ now, a = 7 , b = 5 ∗/ 13
  • 16.
    Variables passing outof scope • What is wrong with this code? #include < stdio . h> char get_message ( ) { ∗ char msg [ ] = "Aren’t pointers fun?" ; return msg; } int main ( void ) { char s t r i n g = get_message ( ) ; ∗ puts ( s t r i n g ) ; return 0; } 14
  • 17.
    Variables passing outof scope • What is wrong with this code? #include < stdio . h> char get_message ( ) { ∗ char msg [ ] = "Aren’t pointers fun?" ; return msg; } int main ( void ) { char s t r i n g = get_message ( ) ; ∗ puts ( s t r i n g ) ; return 0; } • Pointer invalid after variable passes out of scope 14
  • 18.
    6.087 Lecture 5– January 15, 2010 Review Pointers and Memory Addresses Physical and Virtual Memory Addressing and Indirection Functions with Multiple Outputs Arrays and Pointer Arithmetic Strings String Utility Functions Searching and Sorting Algorithms Linear Search A Simple Sort Faster Sorting Binary Search 15
  • 19.
    Arrays and pointers •Primitive arrays implemented in C using pointer to block of contiguous memory • Consider array of 8 ints: int arr [8]; • Accessing arr using array entry operator: int a = arr [0]; • arr is like a pointer to element 0 of the array: int ∗pa = arr; int ∗pa = &arr[0]; ⇔ • Not modifiable/reassignable like a pointer 15
  • 20.
    The sizeof() operator •For primitive types/variables, size of type in bytes: int s = sizeof(char); /∗ == 1 ∗/ double f; /∗ sizeof(f) == 8 ∗/ (64-bit OS) • For primitive arrays, size of array in bytes: int arr [8]; /∗ sizeof(arr) == 32 ∗/ (64-bit OS) long arr [5]; /∗ sizeof(arr) == 40 ∗/ (64-bit OS) • Array length: /∗ needs to be on one l i n e when implemented ∗/ #define array_length ( arr ) ( sizeof ( arr ) == 0 ? 0 : sizeof ( arr ) / sizeof ( ( arr ) [ 0 ] ) ) • More about sizeof() next week. . . 16
  • 21.
    Pointer arithmetic • Supposeint ∗pa = arr; • Pointer not an int, but can add or subtract an int from a pointer: pa + i points to arr[i] • Address value increments by i times size of data type Suppose arr[0] has address 100. Then arr[3] has address 112. • Suppose char ∗ pc = (char ∗)pa; What value of i satisfies (int ∗)(pc+i) == pa + 3? 17
  • 22.
    Pointer arithmetic • Supposeint ∗pa = arr; • Pointer not an int, but can add or subtract an int from a pointer: pa + i points to arr[i] • Address value increments by i times size of data type Suppose arr[0] has address 100. Then arr[3] has address 112. • Suppose char ∗ pc = (char ∗)pa; What value of i satisfies (int ∗)(pc+i) == pa + 3? i = 12 • 17
  • 23.
    6.087 Lecture 5– January 15, 2010 Review Pointers and Memory Addresses Physical and Virtual Memory Addressing and Indirection Functions with Multiple Outputs Arrays and Pointer Arithmetic Strings String Utility Functions Searching and Sorting Algorithms Linear Search A Simple Sort Faster Sorting Binary Search 18
  • 24.
    Strings as arrays •Strings stored as null-terminated character arrays (last character == ’0’) • Suppose char str [] = "This is a string."; and char ∗ pc = str ; • Manipulate string as you would an array ∗(pc+10) = ’S’; puts(str ); /∗ prints "This is a String ." ∗/ 18
  • 25.
    String utility functions •String functions in standard header string.h • Copy functions: strcpy(), strncpy() char ∗ strcpy( strto ,strfrom ); – copy strfrom to strto char ∗ strncpy(strto ,strfrom,n); – copy n chars from strfrom to strto • Comparison functions: strcmp(), strncmp() int strcmp(str1,str2 ); – compare str1, str2; return 0 if equal, positive if str1>str2, negative if str1<str2 int strncmp(str1,str2,n); – compare first n chars of str1 and str2 • String length: strlen() int strlen ( str ); – get length of str 19
  • 26.
    More string utilityfunctions • Concatenation functions: strcat(), strncat() char ∗ strcat ( strto ,strfrom ); – add strfrom to end of strto char ∗ strncat( strto ,strfrom,n); – add n chars from strfrom to end of strto • Search functions: strchr(), strrchr() char ∗ strchr( str ,c); – find char c in str, return pointer to first occurrence, or NULL if not found char ∗ strrchr ( str ,c); – find char c in str, return pointer to last occurrence, or NULL if not found • Many other utility functions exist. . . 20
  • 27.
    6.087 Lecture 5– January 15, 2010 Review Pointers and Memory Addresses Physical and Virtual Memory Addressing and Indirection Functions with Multiple Outputs Arrays and Pointer Arithmetic Strings String Utility Functions Searching and Sorting Algorithms Linear Search A Simple Sort Faster Sorting Binary Search 21
  • 28.
    Searching and sorting •Basic algorithms • Can make good use of pointers • Just a few examples; not a course in algorithms • Big-O notation 21
  • 29.
    Searching an array •Suppose we have an array of int’s int arr [100]; /∗ array to search ∗/ • Let’s write a simple search function: int linear_search ( int val ) { ∗ int parr , parrend = arr + array_length ( arr ) ; ∗ ∗ for ( parr = arr ; parr < parrend ; parr ++) { i f (∗ parr == val ) return parr ; } return NULL; } 22
  • 30.
    A simple sort •A simple insertion sort: O(n2) • iterate through array until an out-of-order element found insert out-of-order element into correct location • • repeat until end of array reached • Split into two functions for ease-of-use int arr [ 1 0 0 ] ; /∗ array to sort ∗/ void shift_element ( unsigned int i ) { /∗ do i n s e r t i o n of out−of−order element ∗/ } void i n s e r t i o n _ s o r t ( ) { /∗ main i n s e r t i o n sort loop ∗/ /∗ c a l l shift_element ( ) f o r each out−of−order element ∗/ } 23
  • 31.
    Shifting out-of-order elements •Code for shifting the element /∗ move previous elements down u n t i l i n s e r t i o n point reached ∗/ void shift_element ( unsigned int i ) { int ivalue ; /∗ guard against going outside array ∗/ for ( ivalue = arr [ i ] ; i && arr [ i −1] > ivalue ; i −−) arr [ i ] = arr [ i −1]; /∗ move element down ∗/ arr [ i ] = ivalue ; /∗ i n s e r t element ∗/ } 24
  • 32.
    Insertion sort • Maininsertion sort loop /∗ i t e r a t e u n t i l out−of−order element found ; s h i f t the element , and continue i t e r a t i n g ∗/ void i n s e r t i o n _ s o r t ( void ) { unsigned int i , len = array_length ( arr ) ; for ( i = 1; i < len ; i ++) i f ( arr [ i ] < arr [ i −1]) shift_element ( i ) ; } • Can you rewrite using pointer arithmetic instead of indexing? 25
  • 33.
    Quicksort • Many fastersorts available (shellsort, mergesort, quicksort, . . . ) • Quicksort: O(n log n) average; O(n2) worst case • choose a pivot element • move all elements less than pivot to one side, all elements greater than pivot to other • sort sides individually (recursive algorithm) • Implemented in C standard library as qsort() in stdlib.h 26
  • 34.
    Quicksort implementation • Selectthe pivot; separate the sides: void quick_sort ( unsigned int l e f t , unsigned int r i g h t ) { unsigned int i , mid ; int pivot ; i f ( l e f t >= r i g h t ) return ; /∗ nothing to sort ∗/ /∗ pivot i s midpoint ; move to l e f t side ∗/ swap( arr + l e f t , arr + ( l e f t + r i g h t ) / 2 ) ; pivot = arr [ mid = l e f t ] ; /∗ separate i n t o side < pivot ( l e f t +1 to mid ) and side >= pivot ( mid+1 to r i g h t ) ∗/ for ( i = l e f t +1; i <= r i g h t ; i ++) i f ( arr [ i ] < pivot ) swap( arr + ++mid , arr + i ) ; [Kernighan and Ritchie. The C Programming Language. 2nd ed. Prentice Hall, 1988.] © Prentice Hall. All rights reserved. This content is excluded from our Creative Commons license. For more information, see http://ocw.mit.edu/fairuse. 27
  • 35.
    Quicksort implementation • Restorethe pivot; sort the sides separately: /∗ restore pivot p os it io n ∗/ swap( arr + l e f t , arr +mid ) ; /∗ sort two sides ∗/ i f ( mid > l e f t ) quick_sort ( l e f t , mid −1); i f ( mid < r i g h t ) quick_sort ( mid+1 , r i g h t ) ; } • Starting the recursion: quick_sort(0, array_length(arr) − 1); [Kernighan and Ritchie. The C Programming Language. 2nd ed. Prentice Hall, 1988.] © Prentice Hall. All rights reserved. This content is excluded from our Creative Commons license. For more information, see http://ocw.mit.edu/fairuse. 28
  • 36.
    Discussion of quicksort •Not stable (equal-valued elements can get switched) in present form • Can sort in-place – especially desirable for low-memory environments • Choice of pivot influences performance; can use random pivot • Divide and conquer algorithm; easily parallelizeable • Recursive; in worst case, can cause stack overflow on large array 29
  • 37.
    Searching a sortedarray • Searching an arbitrary list requires visiting half the elements on average • Suppose list is sorted; can make use of sorting information: • if desired value greater than value and current index, only need to search after index • each comparison can split list into two pieces • solution: compare against middle of current piece; then new piece guaranteed to be half the size • divide and conquer! • More searching next week. . . 30
  • 38.
    Binary search • Binarysearch: O(log n) average, worst case: int binary_search ( int val ) { ∗ unsigned int L = 0 , R = array_length ( arr ) , M; while ( L < R) { M = ( L+R−1)/2; i f ( val == arr [M] ) return arr +M; /∗ found ∗/ else i f ( val < arr [M] ) R = M; /∗ in f i r s t h a l f ∗/ else L = M+1; /∗ in second h a l f ∗/ } return NULL; /∗ not found ∗/ } 31
  • 39.
    Binary search • Worstcase: logarithmic time • Requires random access to array memory • on sequential data, like hard drive, can be slow • seeking back and forth in sequential memory is wasteful • better off doing linear search in some cases • Implemented in C standard library as bsearch() in stdlib.h 32
  • 40.
    Summary Topics covered: • Pointers:addresses to memory • physical and virtual memory • arrays and strings • pointer arithmetic • Algorithms • searching: linear, binary • sorting: insertion, quick 33
  • 41.
    MIT OpenCourseWare http://ocw.mit.edu 6.087 PracticalProgramming in C January (IAP) 2010 For information about citing these materials or our Terms of Use,visit: http://ocw.mit.edu/terms.