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

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

Dec 21, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CSSE221: Software Dev. Honors Day 28 Announcements Announcements Simulation grades coming back Simulation grades coming back All C Projects due Friday.

CSSE221: Software Dev. Honors CSSE221: Software Dev. Honors Day 28Day 28

AnnouncementsAnnouncements Simulation grades coming backSimulation grades coming back All C Projects due Friday night, 11:59 All C Projects due Friday night, 11:59

pmpm Final Exam next Monday, 6-10 pm, Final Exam next Monday, 6-10 pm,

here.here.

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

Thursday’s daily quizThursday’s daily quiz

Show me for completion credit.Show me for completion credit. Check in your code to your personal Check in your code to your personal

repos now. (Any checkins after we repos now. (Any checkins after we start going over it will be too late.)start going over it will be too late.)

Let’s go over together.Let’s go over together.

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

#3: Swap#3: Swap

int main() {int main() {

int a = 10; b = 20; int a = 10; b = 20;

swap(swap(&&a, a, &&b)b)

}}

void swap(int* x, int* void swap(int* x, int* y) {y) {

int temp = *x;int temp = *x;

*x = *y;*x = *y;

*y = temp;*y = temp;

}}

10

20

a b

22ff60 22ff5c

22ff60

22ff5c

x y

Key concept: this is still call-by-value: the parameters, x and y, didn’t change. Do you see why?

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

#4: Scanf#4: Scanf

4a. What would happen if scanf 4a. What would happen if scanf expected and received regular expected and received regular variables instead of addresses? variables instead of addresses?

4b. What happens with the real 4b. What happens with the real scanf if we forget the &?scanf if we forget the &?

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

#5: What’s wrong with #5: What’s wrong with this code?this code?

float *ptr = 0;float *ptr = 0;

printf("%4.2f\n", *ptr);printf("%4.2f\n", *ptr);

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

#6: Pointer arithmetic#6: Pointer arithmetic

What is another expression for What is another expression for element ar[5] using pointers?element ar[5] using pointers?

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

#7: Dynamic memory#7: Dynamic memory

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

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

Any questions?Any questions?

on pointers?on pointers? on C Projects #1-3? on C Projects #1-3?

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

This week: More CThis week: More C

Monday:Monday: Strings in C (capsule)Strings in C (capsule) ??????

Tuesday:Tuesday: Linked list implementation in CLinked list implementation in C

Thursday:Thursday: Wrap-up C unitWrap-up C unit Opportunity for questions about final Opportunity for questions about final

examexam

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

String – an array of String – an array of characterscharacters

___________________ /* declare a string called name ___________________ /* declare a string called name

of 10 characters */of 10 characters */

/* Initialize “name” to “ALICE” *//* Initialize “name” to “ALICE” */

/* Print “name” to the screen – Would you print all 10 /* Print “name” to the screen – Would you print all 10 characters or only the first 5 characters? characters or only the first 5 characters? Hint: use %sHint: use %s */ */

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

String – an array of String – an array of characterscharacters

_char name[10];___ /*declare a string called name _char name[10];___ /*declare a string called name of of

10 10 characters */characters */

/* Initialize “name” to “ALICE” *//* Initialize “name” to “ALICE” */name[0] = ‘A’; name[1] = ‘l’; name[2] = ‘i’; name[3] = ‘c’; name[0] = ‘A’; name[1] = ‘l’; name[2] = ‘i’; name[3] = ‘c’; name[4] = ‘e’;name[4] = ‘e’;name[5] = ‘\0’;name[5] = ‘\0’;

/* Print “name” to the screen – Would you print all 10 /* Print “name” to the screen – Would you print all 10 characters or only the first 5 characters? characters or only the first 5 characters? Hint: use %sHint: use %s */ */

printf(“%s”, name);printf(“%s”, name);

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

Strings – character arrayStrings – character array/* Declaring and Initializing strings *//* Declaring and Initializing strings */char name[ ] = {‘A’,’l’,’i’,’c’,’e’,’\0’}; char name[ ] = {‘A’,’l’,’i’,’c’,’e’,’\0’}; char name [ ] = “Alice”; char name [ ] = “Alice”; char name [6] = {‘A’,’l’,’i’,’c’,’e’,’\0’}; char name [6] = {‘A’,’l’,’i’,’c’,’e’,’\0’};

/* Using scanf read an input string value and save it in /* Using scanf read an input string value and save it in “name”*/“name”*/

scanf(____________, _________);scanf(____________, _________);

An exception for scanf:

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

Strings – character arrayStrings – character array/* Declaring and Initializing strings *//* Declaring and Initializing strings */char name[ ] = {‘A’,’l’,’i’,’c’,’e’,’\0’}; char name[ ] = {‘A’,’l’,’i’,’c’,’e’,’\0’}; char name [ ] = “Alice”; char name [ ] = “Alice”; char name [6] = {‘A’,’l’,’i’,’c’,’e’,’\0’}; char name [6] = {‘A’,’l’,’i’,’c’,’e’,’\0’};

/* Using scanf read an input string value and save it in /* Using scanf read an input string value and save it in “name”*/“name”*/

scanf(”%s”, &name);scanf(”%s”, &name);

An exception for scanf:

No ampersand

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

More arrays - StringsMore arrays - Strings

char name[10]; //declarationchar name[10]; //declarationname = {‘A’,’l’,’i’,’c’,’e’,’\0’}; //initialization name = {‘A’,’l’,’i’,’c’,’e’,’\0’}; //initialization

/* ’\0’= end of string /* ’\0’= end of string */*/

char name [] = “Alice”; //declaration and initializationchar name [] = “Alice”; //declaration and initialization

char name [] = {‘A’,’l’,’i’,’c’,’e’,’\0’}; // dittochar name [] = {‘A’,’l’,’i’,’c’,’e’,’\0’}; // ditto

scanf(“%s”,scanf(“%s”,namename); //Initialization); //Initialization // ERROR: scanf(“%s”,&name);// ERROR: scanf(“%s”,&name);

printf(“%s”, name); /* print until ‘\0’ */printf(“%s”, name); /* print until ‘\0’ */

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

What is the output of the following code segment? Use the What is the output of the following code segment? Use the “man” pages to determine what “strlen” does?“man” pages to determine what “strlen” does?

char name[ ] = {‘A’,’l’,’e’,’x’,’\0’,’a’,’n’,’d’,’e’,’r’);char name[ ] = {‘A’,’l’,’e’,’x’,’\0’,’a’,’n’,’d’,’e’,’r’);

printf(“ The length of the string is %d\n”, strlen(name));printf(“ The length of the string is %d\n”, strlen(name));

printf(“ And the string is %s\n”,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!

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

What is the output of the following code segment? Use the man pages to What is the output of the following code segment? Use the man pages to determine what “strlen” does?determine what “strlen” does?

char name[ ] = {‘A’,’l’,’e’,’x’,’\0’,’a’,’n’,’d’,’e’,’r’);char name[ ] = {‘A’,’l’,’e’,’x’,’\0’,’a’,’n’,’d’,’e’,’r’);printf(“ The length of the string is %d\n”, strlen(name));printf(“ The length of the string is %d\n”, strlen(name));printf(“ And the string is %s\n”,name);printf(“ And the string is %s\n”,name);

The length of the string is 4.The length of the string is 4.And the string is Alex.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!

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

Other string functionsOther string functions

Functions to operate on stringsFunctions to operate on strings

________________________ compare two strings________________________ compare two strings

________________________ break a string into tokens________________________ break a string into tokens

________________________ finds the first occurrence of a character in a ________________________ finds the first occurrence of a character in a

stringstring

________________________ make a copy of a string________________________ make a copy of a string

What are the libraries that handle strings and characters? What are the libraries that handle strings and characters?

Do you need a special flag when compiling a program that uses a function Do you need a special flag when compiling a program that uses a function fromfrom

either string library? either string library?

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

Other string functionsOther string functions

Functions to operate on stringsFunctions to operate on strings

_strcmp, strncmp________ compare two strings_strcmp, strncmp________ compare two strings

_strtok__________________ break a string into tokens_strtok__________________ break a string into tokens

_strchr _________________ finds the first occurrence of a character in a _strchr _________________ finds the first occurrence of a character in a

stringstring

_strcpy_________________ make a copy of a string_strcpy_________________ make a copy of a string

What are the two string libraries? What are the two string libraries?

string.h strings.h stdlib.h ctype.hstring.h strings.h stdlib.h ctype.h

Do you need a special flag when compiling a program that uses a function Do you need a special flag when compiling a program that uses a function fromfrom

either string library? either string library?

We can get this information from the “man” pages. And, the answer is that a We can get this information from the “man” pages. And, the answer is that a special flag is not needed. special flag is not needed.

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

Strings contd.Strings contd.

Functions to operate on stringsFunctions to operate on strings• strcpy, strncpy, strcmp, strncmp, strcpy, strncpy, strcmp, strncmp,

strcat, strncat, substr, strlen,strtokstrcat, strncat, substr, strlen,strtok• #include <strings.h> or <string.h> #include <strings.h> or <string.h>

at program startat program start CAUTION: C allows strings of any length CAUTION: C allows strings of any length

to be stored. Characters beyond the end to be stored. Characters beyond the end of the array will overwrite data in memory of the array will overwrite data in memory following the array.following the array.

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

BreakBreak

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

Array of PointersArray of Pointers

Variable length stringsVariable length stringschar *card[4]; char *card[4]; // card[4] => array of 4 elements// card[4] => array of 4 elements

//char* => element is a pointer to a //char* => element is a pointer to a character.character.

// card[4] => array of 4 char pointers// card[4] => array of 4 char pointers

40004000

40044004

40084008

40124012

card[0]

card[1]

card[2]

card[3]

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

card[0] = (char*)malloc(6*sizeof(char));card[0] = (char*)malloc(6*sizeof(char));

card[1] = (char*)malloc(3*sizeof(char)); card[1] = (char*)malloc(3*sizeof(char)); and so on.and so on.

Static allocation of a 2D array:Static allocation of a 2D array:

char card[4][10]; char card[4][10]; //waste of //waste of spacespace

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

For the following code segment, complete the diagrams provided to For the following code segment, complete the diagrams provided to graphically represent the pointer creations and assignments. Again, graphically represent the pointer creations and assignments. Again, line numbers are provided. You may assume that the malloc on line 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 2 allocates space starting at address 5000 and the malloc on line 3 allocates space starting at address 6000.allocates space starting at address 6000.

char *card[4]; char *card[4];

card[0] = (char*)malloc(8*sizeof(char)); ----- line 2card[0] = (char*)malloc(8*sizeof(char)); ----- line 2

card[3] = (char*)malloc(9*sizeof(char)); ------- line 3card[3] = (char*)malloc(9*sizeof(char)); ------- line 3

strcpy(card[0],”hearts”); ------- line 4strcpy(card[0],”hearts”); ------- line 4

strcpy(card[3],”diamonds”); ------ line 5strcpy(card[3],”diamonds”); ------ line 5

40004000

40044004

40084008

40124012

card[0]

card[1]

card[2]

card[3]