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
Embed
CSSE221: Software Dev. Honors Day 28 Announcements Announcements Simulation grades coming back Simulation grades coming back All C Projects due Friday.
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.
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.
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.)
Key concept: this is still call-by-value: the parameters, x and y, didn’t change. Do you see why?
#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 &?
#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);
#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?
#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:
Any questions?Any questions?
on pointers?on pointers? on C Projects #1-3? on C Projects #1-3?
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
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 */ */
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
/* 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);
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”*/
printf(“%s”, name); /* print until ‘\0’ */printf(“%s”, name); /* print until ‘\0’ */
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?
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!
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!
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?
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?
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.
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.
BreakBreak
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
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
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