Unformatted text preview:

1CMSC 212 – S07 (lect 4)Announcementsz Program #1– Due in one weekz Email with– Schedule conflicts for midterm– DSS needsz Reading– Chapter 7 & 8• skip 8.1.4– Chapter 9 & 10 (Thursday)2CMSC 212 – S07 (lect 4)Switch Statementswitch (expression) {case constant-expression:statement;break;….default:statement;break;}zExecution starts at the constant-expression equal to the expression– Without the break, will continue to next statement!!!z Default constant expression matches anything else3CMSC 212 – S07 (lect 4)Example of Switch Statementswitch (command) {case 'A':add_entry();break;case 'D':delete_entry();break;case 'P':print_entry();break;case 'E':edit_entry();break;default:printf("Error");break;}4CMSC 212 – S07 (lect 4)Pointer Variablesz Hold an address of something else– its location in memory– usually an address to a specified type of spacez type * varname;–int* a;– char * b;– int *a, b;– int *x, *y;z Get an address by using a &– int a = 6;– printf(“%d is at %p\n”, a, &a);z Dereference also using a star–inta, *b;–a = 7;–b = &a;– printf(“%d and %d \n”, a, *b);5CMSC 212 – S07 (lect 4)Functionsz type name (parameters) block– type: return type for the function– name: name of the function– parameters: a comma separated list of types and names– block: the code to runz return statement: return expression;– terminates a function at that point– expression is the value returned by the function6CMSC 212 – S07 (lect 4)Function Exampleint find(int data[10], int value) {for (int i=0; i < 10; i++) {if (data[i] == value) {return i;}}return -1;}7CMSC 212 – S07 (lect 4)Function Argumentsz Comma separated list of valuesz All parameters are passed by value– called function can modify values– arrays are passed by the address of their 0th element• modifying elements of array seen by calling functionz Can pass address of variable to change valuesvoid foo(int *a) {*a = 3;}…int x;foo(&x);…8CMSC 212 – S07 (lect 4)Recursive Functionsz Functions that call themselves – can be indirect: a calls b calls az Example:void binary_to_ascii(unsigned int value) {unsigned int quotient;quotient = value / 10;if (quotient != 0) {binary_to_ascii(quotient);}printf("%c", value % 10 + '0');}9CMSC 212 – S07 (lect 4)Passing Parameters to Mainz int main(int argc, char *argv[])– Normal declaration of main– argc – number of command line arguments• Includes command itself– argv – array of character strings of command argumentsz Example– From command line: % myprog –file myfile– In the program:• argc == 3• argv[0] contains “myprog”• argv[1] contains “-file”• argv[2] contains “myfile”10CMSC 212 – S07 (lect 4)Standard Type Definitionsz Several Types are defined in C's libraries– Defined in stddef.h– size_t - unsigned integer value (often int or long)– ptrdiff_t - signed value (used when subtracting pointers)z Used by several standard routines– sizeof(<type>) returns a size_t for example11CMSC 212 – S07 (lect 4)C-Stringsz Definition– An array of characters– Where the used portion is terminated by a null characterz <string.h>– Library that acts on C-strings– Most will crash if given something that does not fit the definition abovez Creating and Initializing a stringchar name1[5] = {‘J’,’e’,’f’, ‘f’,’\0’};char name2[14] = “Hollingsworth”;z length of the string and the sizeof operator– sizeof operator tells the size of the variable or type– strlen uses the definition of C-string to find number of used characters12CMSC 212 – S07 (lect 4)Stringsz Zero or more characters followed by null char ‘\0’– also called NUL– not counted as part of string– string.h defines prototypes for string routinesz Copying Strings– size_t strlen(char const *str);• returns count of characters in str• up to but not including the null character– char *strncpy(char *dst, char const *src, size_t len);• copy src to dst• copy until ‘\0’ in src or at most len characters• pad extra characters will ‘\0’• Safety tip: dst[len-1] = '\0'; to force termination of new string– char *strncat(char *dst, char const *src, size_t len);• append src onto the end of dst• always appends NUL to end of dst string13CMSC 212 – S07 (lect 4)String Functionsz Comparison– int strncmp(char const *s1, char const *s2, size_t len);• returns 0 if string equal up to len• returns a negative value if s1 is less than s2• returns a positive value if s1 is greater than s2z Searching– char *strchr(char const *str, int ch);– char *strrchr(char const *str, int ch);• finds the first occurance of ch in str• strrchr finds the last occurance• returns NULL if not found– char *strstr(char const *s1, char const *s2);• find the first occurrence of s2 in s114CMSC 212 – S07 (lect 4)String Functions Exampleschar string[] = "this is a test string";char *ans;int length;length = strlen(string); /* returns 21 */ans = strchr(string, 'h'); /* returns string + 1 */ans = strrchr(string, 't'); /* returns string + 16 */ans = strstr(string, "test"); /* returns string + 10


View Full Document

UMD CMSC 212 - Lecture Slides

Download Lecture Slides
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture Slides and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture Slides 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?