Strings Page 1 How would this be stored
Strings Page 1 How would this be stored in RAM ? ? Assume that the base address of chararray (== &chararray[0]) is 1200: 1200 1201 1202 1203 1204 1205 1206 H e l l o 00011011010 1201 1202 1203 1204 72 101 108 111 1200 1201 1202 1203 1204 ‘Garbage’ 01001000 01100101 01101111 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft
Strings Page 2 This is exactly the same as numeric arrays !!! True -- Except for one difference Consider the sentences: The quality of mercy is not strained. It droppeth as the gentle rains from the heavens upon the place beneath. How many characters are there in the sentences (don’t forget to count spaces and special characters) ? ? --- There actually 108 --There a few points to be made: • Do we really want to count how many characters there are ? ? • Do we really care about the positions (offsets from the base address) of the characters ? ? Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft
Strings Page 3 What’s the Solution ? ? ? What do we need to know ? ? ? • The base address of the array • We can readily determine this by referring to the variable name (in our case, chararray == &chararray[0]) • Where the string ends How do we know where a string ends ? ? ? Right now, we don’t BUT …… if we were to add an additional character at the end of the string, we could check to see if we had reached the end of the string Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft
Strings Page 4 What Character ? ? ? In c, we add a NULL (‘ ’) character at the end of the array Rewriting our (previous) c code: void main() { int i; chararray[6]; chararray[0] = ‘H’; chararray[1] = ‘e’; chararray[2] = ‘l’; chararray[3] = ‘l’; chararray[4] = ‘o’; chararray[5] = ‘ ’; NOTICE: • We must allocate 1 -byte more than anticipate Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft
Strings Page 5 How does this make things easier for us ? ? ? In a number of respects: • Because we know that a NULL character will be added at the end of a string, we do NOT have to declare how many characters are in a string when we initialize We could have declared our string as: void main() { int i; chararray[] = “Hello”; Which would have the same effect as our previous code • We could have also printed our string with the command: puts(chararray); OR printf(“%sn”, chararray); Which would have the same effect as the code: for (i = 0; i < 5; i++) printf(“%c”, chararray[i]); Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft
Strings Page 6 How would this be stored in RAM ? ? Again, Assume that the base address of chararray (== &chararray[0]) is 1200: 1200 1201 1202 1203 1204 1205 1206 H e l l o 11011010 1201 1202 1203 1204 1205 72 101 108 111 0 1201 1202 1203 1204 1205 ‘Garbage’ 01001000 01100101 01101111 0000 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft
Strings Page 7 How does puts work ? ? ? • puts is a standard function found in <stdio. h> • The function receives a base address and continues printing the elements of the array until it encounters a NULL (‘ ’) character • If we were to pass the base address of our string (chararray) as: puts(chararray); The C function necessary might appear as: void puts (char *base) { // we could have used: int i = 0; while (*base != '