Download - Introduction to ANSI C Part Three
Intro to C Part 3: © Walter Milner 2005: Slide 1
Introduction to ANSI CIntroduction to ANSI CPart ThreePart Three
Strings Strings in structs File handling Linked list example
Intro to C Part 3: © Walter Milner 2005: Slide 2
Recall – strings stored in memory as character codes terminated by 0
So “ABC” stored in 4 locations - maybe: 65, 66, 67, 0 Character code might be ASCII, or UNICODE or others –
not defined in C
StringsStrings
Intro to C Part 3: © Walter Milner 2005: Slide 3
Strings as char arraysStrings as char arrays For example,char myString[4];myString[0]='A'; // use ’ for charsmyString[1]='B';myString[2]='C';myString[3]=0;printf("%s\n",myString);
What would happen if we replaced the 4th assignment by:myString[3]=65;
Intro to C Part 3: © Walter Milner 2005: Slide 4
Strings as pointer to charStrings as pointer to char Recall an array is the same as a pointer to the start of a block
of memory Equivalent to last example:char * myString;myString= (char *) malloc(4);*myString='A';*(myString+1)='B';*(myString+2)='C';*(myString+3)=0;printf("%s\n",myString);
Intro to C Part 3: © Walter Milner 2005: Slide 5
Input and output of stringsInput and output of strings
char * myString;myString= (char *) malloc(10);scanf("%s",myString);printf("%s\n",myString);
NB see health warning on next slide
Intro to C Part 3: © Walter Milner 2005: Slide 6
Exercise on string I/OExercise on string I/O Run the following program, but type in more than 10
characters:#include <stdio.h>
int main(){int x = 5;char myString[10];scanf("%s",myString);printf("%s\n",myString);printf("%i\n",x);return 0;
}Explain what happens
Intro to C Part 3: © Walter Milner 2005: Slide 7
One fix to string input One fix to string input problemproblem
char myString[10];scanf("%10s",myString);
Intro to C Part 3: © Walter Milner 2005: Slide 8
Typical char * processing: Typical char * processing: count the capital letterscount the capital letters
int capCount = 0;char * myString = "Born in the USA";char * where;where = myString;while (*where!=0){if (*where>='A' && *where<='Z')capCount++;where++;}printf("%i\n",capCount);return 0;
Intro to C Part 3: © Walter Milner 2005: Slide 9
String exerciseString exercise Write code like the last example to count the spaces in
a string
Intro to C Part 3: © Walter Milner 2005: Slide 10
String constantsString constants For examplechar * myString = “What a fine day!”;At compile time the assembler – Puts aside enough memory to hold the stringPuts appropriate data there (“What a fine..Generates code to make myString point to that
memory
Intro to C Part 3: © Walter Milner 2005: Slide 11
string functionsstring functions These are in <string.h> strcpy(a,b) - copy string b to a,
including 0 - return a strcat(a,b) - concatenate string b to a strcmp(a,b) - compare - <0 if a<b, 0 if
a==b, >0 if a > b strchr(s,c) - return pointer to first
occurence of char c in string s strlen(s) - return length of string s plus others
Intro to C Part 3: © Walter Milner 2005: Slide 12
Using string functionsUsing string functionschar * s1 = "hello";char * s2;int i;char * where;s2=calloc(80, sizeof(char)); // get some spacestrcpy(s2, s1); // copy hello into iti = strcmp(s2, s1); // i becomes 0strcat(s2,s1); // s2 becomes hellohelloi = strlen(s2); // i is 10where = strchr(s2, 'l'); // *where is llohello
Intro to C Part 3: © Walter Milner 2005: Slide 13
String exerciseString exercise
1. Write a program to test strlen, strcpy, strcat and strcmp 2. Write your own version of strlen
Intro to C Part 3: © Walter Milner 2005: Slide 14
String constants againString constants again In many implementations, string constants are in ‘non-
modifiable memory’ to prevent you doing something like
char * s = “Hello”;strcat(s, “Goodbye”);
Correct version -char * s;s = calloc(256, sizeof(char));strcpy(s,"Hello");strcat(s,"Goodbye");printf("%s\n",s);
Intro to C Part 3: © Walter Milner 2005: Slide 15
Tricky string processingTricky string processing// count Capitals in a string again..int capCount = 0;char * myString; char * where;myString=malloc(50);strcpy(myString,"Born in the USA");where=myString;while (*where++)
if (*(where-1)>='A' && *(where-1)<='Z')capCount++;
printf("%i\n",capCount);
Intro to C Part 3: © Walter Milner 2005: Slide 16
Structs again – Structs again – typical codetypical codestruct employee
{int payrollnumber;int deptCode;
};
int main(){
struct employee employee1;employee1.payrollnumber=38;printf("%i",employee1.payrollnumber);return 0;
}
Intro to C Part 3: © Walter Milner 2005: Slide 17
Struct with string fieldStruct with string fieldstruct employee{
char * name;int payrollnumber;
};
void showdetails(struct employee someone){
printf("Name = %s\n",someone.name);printf("Payroll number = %i\n",someone.payrollnumber);return;
}
int main(){
struct employee john;john.name = "John Brown";john.payrollnumber = 123;showdetails(john);return 0;
}
Intro to C Part 3: © Walter Milner 2005: Slide 18
struct employee{
char * name;int deptCode;
};int main(){
struct employee * employeePtr;employeePtr = calloc(1, sizeof(struct
employee));employeePtr->deptCode=38;employeePtr->name=malloc(50);strcpy(employeePtr->name,"John Brown");printf("%s",employeePtr->name);return 0;
}
Structs with strings and Structs with strings and dynamic memorydynamic memory
Intro to C Part 3: © Walter Milner 2005: Slide 19
Struct exerciseStruct exercise Modify previous code to have an array of 100
employees. Give data to each one.
Intro to C Part 3: © Walter Milner 2005: Slide 20
FilesFiles ‘Normal’ file is stored on disc (was on tape) or other
non-volatile medium C also uses files called stdin and stdout, which default to
keyboard and screen So input and output is also file-handling Functions protoyped in stdio.h
Intro to C Part 3: © Walter Milner 2005: Slide 21
File handlingFile handling
Access is through a pointer to a file declared likeFILE * fp;
File handling is done in a sandwich:
open the file
read or write it
close it
Intro to C Part 3: © Walter Milner 2005: Slide 22
Writing to file 1Writing to file 1
FILE * myFile;myFile = fopen("test1.dat", "wb");fwrite("Hello",5,1,myFile);fclose(myFile);
External filename
Mode – write binary – deletes existing file
Pointer to data, items to write, size each item, filepointer
Intro to C Part 3: © Walter Milner 2005: Slide 23
Writing integer data to fileWriting integer data to fileFILE * myFile;int * data = calloc(10, sizeof(int));int i;for (i=0; i<10; i++)
*(data+i)=i;myFile = fopen("test1.dat", "wb");fwrite(data,10,sizeof(int),myFile);fclose(myFile);
Intro to C Part 3: © Walter Milner 2005: Slide 24
Reading integer dataReading integer dataFILE * myFile;int * data = calloc(10, sizeof(int));int i;
myFile = fopen("test1.dat", "rb");fread(data,10,sizeof(int),myFile);for (i=0; i<10; i++)
printf("%i ",*(data+i));fclose(myFile);
Intro to C Part 3: © Walter Milner 2005: Slide 25
File read write exerciseFile read write exercise Define a struct (use the employee one) Give a struct variable some data Write it into a disc file Read the disc file back and display it
Intro to C Part 3: © Walter Milner 2005: Slide 26
Reading text filesReading text filesFILE * myFile;char * myBuffer=malloc(80);
myFile = fopen("test2.dat", "r");while (1){
fgets(myBuffer, 80, myFile);
if (feof(myFile)) break;printf("%s",myBuffer);
} fclose(myFile);
Intro to C Part 3: © Walter Milner 2005: Slide 27
Text file exerciseText file exercise Adapt the last example to write a program which counts
the lines in a text file.
Intro to C Part 3: © Walter Milner 2005: Slide 28
File open modes -File open modes -"r" -- to open an existing text file for reading "w" -- to create a text file or to open and truncate an existing text file, for writing "a" -- to create a text file or to open an existing text file, for writing. The file-position indicator is positioned at the end of the file before each write "rb" -- to open an existing binary file for reading "wb" -- to create a binary file or to open and truncate an existing binary file, for writing "ab" -- to create a binary file or to open an existing binary file, for writing. The file-position indicator is positioned at the end of the file (possibly after arbitrary null byte padding) before each write "r+" -- to open an existing text file for reading and writing "w+" -- to create a text file or to open and truncate an existing text file, for reading and writing "a+" -- to create a text file or to open an existing text file, for reading and writing. The file-position indicator is positioned at the end of the file before each write "r+b" or "rb+" -- to open an existing binary file for reading and writing "w+b" or "wb+" -- to create a binary file or to open and truncate an existing binary file, for reading and writing "a+b" or "ab+" -- to create a binary file or to open an existing binary file, for reading and writing. The file-position indicator is positioned at the end of the file (possibly after arbitrary null byte padding) before each write
Intro to C Part 3: © Walter Milner 2005: Slide 29
Using strings againUsing strings again Task - take a string like “Have a fine day” and split it
into separate words, one word in each string Where to put the word strings - what data structure? Have an array of pointers to the strings How many elements? First count the words Then get space for the array Then copy each word somewhere, and point each array
element to it
Array of pointers to char
H a v e \0a \0f i n e \0d a y \0
Intro to C Part 3: © Walter Milner 2005: Slide 30
String exampleString examplechar * s1 = "Have a fine day";int i, wordcount=1;char *where, *start, *end;char * * pointerstowords; // note the char * *//count the number of wordsfor (where=s1; *where; where++) if ( (*where) == ' ') wordcount++;// make space for the array of pointerspointerstowords = calloc(wordcount, sizeof(char *));// put all but last word into strings pointed to by arraystart=s1;for (i=0;i<wordcount-1; i++) { // find first space after start end=strchr(start,' '); // make pointer element point to enough memory pointerstowords[i]=calloc(end-start+2, sizeof(char)); // copy correct number of characters strncpy(*(pointerstowords+i), start, end-start+1); // put 0 at end *(pointerstowords[i]+(end-start)+1)='\0'; // next time, start at letter after the SPACE start=end+1; }// for last word.. point to enough memory..pointerstowords[wordcount-1]=calloc(strlen(start)+1, sizeof(char));// copy the wordstrcpy(*(pointerstowords+wordcount-1), start);*(*(pointerstowords+wordcount-1)+strlen(start)+1)='\0';// put 0 at end
Intro to C Part 3: © Walter Milner 2005: Slide 31
Linked listsLinked lists A linked list is a data structure made of a set of nodes,
each containing a pointer to the next node34 21 39
Head pointer
As the program runs, calloc and dispose are used to add and remove nodesIn general, the nodes will not be next to each other in memory
Intro to C Part 3: © Walter Milner 2005: Slide 32
Linked list implementedLinked list implementedstruct node{ int key; struct node * next;};
int main(){ struct node * head; struct node * newnode;
head = calloc(1, sizeof(struct node)); head->key = 123; head->next = NULL;
newnode = calloc(1, sizeof(struct node)); head->next = newnode; newnode->key = 321; newnode->next = NULL; return 0;}
123 321
head newnode