Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSSE221: Software Dev. Honors Day 28 Announcements Announcements Simulation grades coming back Simulation grades coming back All C Projects due Friday.

Similar presentations


Presentation on theme: "CSSE221: Software Dev. Honors Day 28 Announcements Announcements Simulation grades coming back Simulation grades coming back All C Projects due Friday."— Presentation transcript:

1 CSSE221: Software Dev. Honors Day 28 Announcements Announcements Simulation grades coming back Simulation grades coming back All C Projects due Friday night, 11:59 pm All C Projects due Friday night, 11:59 pm Final Exam next Monday, 6-10 pm, here. Final Exam next Monday, 6-10 pm, here.

2 Thursday’s daily quiz Show me for completion credit. Show me for completion credit. Check in your code to your personal repos now. (Any checkins after we start going over it will be too late.) Check in your code to your personal repos now. (Any checkins after we start going over it will be too late.) Let’s go over together. Let’s go over together.

3 #3: Swap int main() { int a = 10; b = 20; swap(&a, &b) } void swap(int* x, int* y) { int temp = *x; *x = *y; *y = temp; } 1020 ab 22ff60 22ff5c 22ff60 22ff5c xy Key concept: this is still call-by-value: the parameters, x and y, didn’t change. Do you see why?

4 #4: Scanf 4a. What would happen if scanf expected and received regular variables instead of addresses? 4a. What would happen if scanf expected and received regular variables instead of addresses? 4b. What happens with the real scanf if we forget the &? 4b. What happens with the real scanf if we forget the &?

5 #5: What’s wrong with this code? float *ptr = 0; printf("%4.2f\n", *ptr);

6 #6: Pointer arithmetic What is another expression for element ar[5] using pointers?

7 #7: Dynamic memory Whenever you allocate memory using ptr = malloc(...), you need also to: Whenever you allocate memory using ptr = malloc(...), you need also to:

8 Any questions? on pointers? on pointers? on C Projects #1-3? on C Projects #1-3?

9 Recap: Declarations Reserve Space Variable declarations reserve space in memory: Variable declarations reserve space in memory: int x; /* reserves enough space for an int, names it x */ int x; /* reserves enough space for an int, names it x */ double d; /* reserves enough space for a double, names it d */ double d; /* reserves enough space for a double, names it d */ Formal parameter declarations do the same: Formal parameter declarations do the same: void average(double sum, int count) {…} void average(double sum, int count) {…} /* reserves enough space for a double (named sum) and an int (named count)*/ /* reserves enough space for a double (named sum) and an int (named count)*/

10 Recap: Variables with "Pointer Types" Store Addresses Besides holding "things" like ints and doubles, variables in C can also hold memory addresses Besides holding "things" like ints and doubles, variables in C can also hold memory addresses Samples: Samples: int *xPtr; /* reserves enough space for an address, names it xPtr, says that xPtr can store the address of another variable that holds an int */ int *xPtr; /* reserves enough space for an address, names it xPtr, says that xPtr can store the address of another variable that holds an int */ double *dPtr; /* reserves enough space for an address, names it dPtr, says that dPtr can store the address of another variable that holds a double */ double *dPtr; /* reserves enough space for an address, names it dPtr, says that dPtr can store the address of another variable that holds a double */

11 Recap: Pointer Operators, & The address operator, &: The address operator, &: &var gives the address where var's value is stored &var gives the address where var's value is stored Examples: Examples: xPtr = &x;/* Read "xPtr gets the address of x" */ xPtr = &x;/* Read "xPtr gets the address of x" */ dPtr = &d; /* Read "dPtr gets the address of d" */ dPtr = &d; /* Read "dPtr gets the address of d" */

12 Recap: Pointer Operators, * Use * two ways: Use * two ways: In type declarations, * says that the name refers to address of something: int *xPtr; double *dPtr; In type declarations, * says that the name refers to address of something: int *xPtr; double *dPtr; In expressions, *var gives the "thing" pointed to by var In expressions, *var gives the "thing" pointed to by var Examples: Examples: printf("%d", *xPtr); printf("%d", *xPtr); *dPtr = 3.14159; *dPtr = 3.14159; The format string, "%d", says that we want to print an int. *xPtr is the thing pointed to by xPtr. That is, *xPtr is the value of x. This says that the thing pointed to by dPtr should get the value 3.14159. So the result is the same as d = 3.14159.

13 Pointer Assignments int x=3, y = 5; int *px = &x; int *py = &y; printf("%d %d\n", x, y); *px = 10; printf("%d %d\n", x, y); px = py; *px = 12; printf("%d %d\n", x, y); /* x is changed */ /* y is changed */

14 This week: More C Monday: Monday: Strings in C (capsule) Strings in C (capsule) ??? ??? Tuesday: Tuesday: Linked list implementation in C Linked list implementation in C Thursday: Thursday: Wrap-up C unit Wrap-up C unit Opportunity for questions about final exam Opportunity for questions about final exam

15 String – an array of characters String – an array of characters ___________________ /* declare a string called name of 10 characters */ of 10 characters */ /* Initialize “name” to “ALICE” */ /* Print “name” to the screen – Would you print all 10 characters or only the first 5 characters? Hint: use %s */

16 String – an array of characters String – an array of characters _char name[10];___ /*declare a string called name of 10 characters */ 10 characters */ /* Initialize “name” to “ALICE” */ name[0] = ‘A’; name[1] = ‘l’; name[2] = ‘i’; name[3] = ‘c’; name[4] = ‘e’; name[5] = ‘\0’; /* Print “name” to the screen – Would you print all 10 characters or only the first 5 characters? Hint: use %s */ printf(“%s”, name);

17 Strings – character array /* Declaring and Initializing strings */ char name[ ] = {‘A’,’l’,’i’,’c’,’e’,’\0’}; char name [ ] = “Alice”; char name [6] = {‘A’,’l’,’i’,’c’,’e’,’\0’}; /* Using scanf read an input string value and save it in “name”*/ scanf(____________, _________); An exception for scanf:

18 Strings – character array /* Declaring and Initializing strings */ char name[ ] = {‘A’,’l’,’i’,’c’,’e’,’\0’}; char name [ ] = “Alice”; char name [6] = {‘A’,’l’,’i’,’c’,’e’,’\0’}; /* Using scanf read an input string value and save it in “name”*/ scanf(”%s”, &name); An exception for scanf: No ampersand

19 More arrays - Strings char name[10]; //declaration name = {‘A’,’l’,’i’,’c’,’e’,’\0’}; //initialization /* ’\0’= end of string */ char name [] = “Alice”; //declaration and initialization char name [] = {‘A’,’l’,’i’,’c’,’e’,’\0’}; // ditto scanf(“%s”,name); //Initialization // ERROR: scanf(“%s”,&name); // ERROR: scanf(“%s”,&name); printf(“%s”, name); /* print until ‘\0’ */

20 What is the output of the following code segment? Use the “man” pages to determine what “strlen” does? char name[ ] = {‘A’,’l’,’e’,’x’,’\0’,’a’,’n’,’d’,’e’,’r’); printf(“ The length of the string is %d\n”, strlen(name)); printf(“ And the string is %s\n”,name); CAUTION: Strings lengths need not match the size of the array. So, always assign sufficient space to hold the longest string for that situation. No out-of-bounds exception!

21 What is the output of the following code segment? Use the man pages to determine what “strlen” does? char name[ ] = {‘A’,’l’,’e’,’x’,’\0’,’a’,’n’,’d’,’e’,’r’); printf(“ The length of the string is %d\n”, strlen(name)); printf(“ And the string is %s\n”,name); The length of the string is 4. And the string is Alex. CAUTION: Strings lengths need not match the size of the array. So, always assign sufficient space to hold the longest string for that situation. No out-of-bounds exception!

22 Other string functions Functions to operate on strings ________________________ compare two strings ________________________ break a string into tokens ________________________ finds the first occurrence of a character in a string string ________________________ make a copy of a string What are the libraries that handle strings and characters? Do you need a special flag when compiling a program that uses a function from either string library?

23 Other string functions Functions to operate on strings _strcmp, strncmp________ compare two strings _strtok__________________ break a string into tokens _strchr _________________ finds the first occurrence of a character in a string string _strcpy_________________ make a copy of a string What are the two string libraries? string.h strings.h stdlib.h ctype.h Do you need a special flag when compiling a program that uses a function from either string library? We can get this information from the “man” pages. And, the answer is that a special flag is not needed.

24 Strings contd. Functions to operate on strings Functions to operate on strings strcpy, strncpy, strcmp, strncmp, strcat, strncat, substr, strlen,strtok strcpy, strncpy, strcmp, strncmp, strcat, strncat, substr, strlen,strtok #include or at program start #include or at program start CAUTION: C allows strings of any length to be stored. Characters beyond the end of the array will overwrite data in memory following the array. CAUTION: C allows strings of any length to be stored. Characters beyond the end of the array will overwrite data in memory following the array.

25 Break

26 Array of Pointers Variable length strings Variable length strings char *card[4]; // card[4] => array of 4 elements //char* => element is a pointer to a character. //char* => element is a pointer to a character. // card[4] => array of 4 char pointers // card[4] => array of 4 char pointers 4000 4004 4008 4012 card[0] card[1] card[2] card[3]

27 card[0] = (char*)malloc(6*sizeof(char)); card[1] = (char*)malloc(3*sizeof(char)); and so on. Static allocation of a 2D array: Static allocation of a 2D array: char card[4][10]; //waste of space

28 For the following code segment, complete the diagrams provided to graphically represent the pointer creations and assignments. Again, line numbers are provided. You may assume that the malloc on line 2 allocates space starting at address 5000 and the malloc on line 3 allocates space starting at address 6000. char *card[4]; card[0] = (char*)malloc(8*sizeof(char)); ----- line 2 card[3] = (char*)malloc(9*sizeof(char)); ------- line 3 strcpy(card[0],”hearts”); ------- line 4 strcpy(card[3],”diamonds”); ------ line 5 4000 4004 4008 4012 card[0] card[1] card[2] card[3]


Download ppt "CSSE221: Software Dev. Honors Day 28 Announcements Announcements Simulation grades coming back Simulation grades coming back All C Projects due Friday."

Similar presentations


Ads by Google