MCIT 593 – Introduction to Computer Systems
WHAT IS A STRING IN C?
Property of Penn Engineering 2
Copyright By PowCoder代写 加微信 powcoder
MCIT 593 – Introduction to Computer Systems
Character Array versus String
There is no actual string type in standard C
• A string is defined as an array of characters terminated by a NULL
• Often you hear it as “NULL terminated string”
• This is a string in C:
• char my_string [4] = {‘T’, ‘o’, ‘m’, ‘\0’} ;
• This is NOT a string in C:
• char not_a_string [] = {‘T’, ‘o’, ‘m’ } ;
• Without the NULL…‘\0’…It is just an array of characters
Property of Penn Engineering 3
MCIT 593 – Introduction to Computer Systems
Character Array versus String
If you wrap a string in quotes i.e. “string”
• It is automatically NULL terminated by the compiler
• A string in double quotes ” ” is called a string literal in C
• This is a string in C (if initializing, you don’t need to specify size):
• char my_string [] = {‘T’, ‘o’, ‘m’, ‘\0’} ;
• Alternate way to define the same string in C:
• char my_string [] = “Tom” ;
• The compiler will add the NULL automatically to the end
• You can think of it like data immediately available in an instruction!
Property of Penn Engineering 4
MCIT 593 – Introduction to Computer Systems
CHARACTER ARRAY VS STRING VS POINTER TO STRING
Property of Penn Engineering 6
MCIT 593 – Introduction to Computer Systems
Character Array versus String versus Pointer to String
A pointer to a string is very different from an array of characters • Recall the difference between a pointer and an array in C:
• A pointer contains a memory address
• An array’s name is like a label for a memory address
• This is a string in C:
• char my_string [] = {‘T’, ‘o’, ‘m’, ‘\0’} ;
• This is a pointer to a string in C:
• char* my_string_ptr = “Tom” ; // this is a pointer to a String
This is very different from the following code:
• char my_string [] = “Tom” ; // this is an array that contains a String
Property of Penn Engineering 7
MCIT 593 – Introduction to Computer Systems
Character Array versus String versus
Pointer to String
int main() {
LC4 Data Memory
(static region)
LC4 Data Memory
(stack region)
char my_string []= “Tom” ;
char* my_string_ptr = “Tom” ;
(my_string) x7FF8
my_string_ptr
my_string[0]
my_string[1]
my_string[2]
my_string[3]
Property of Penn Engineering 8
MCIT 593 – Introduction to Computer Systems
Recall…Storage Classes for Variables in C
• 3possibleplacestostoredatainContheLC4: • TheStack
• localvariables,arguments,returns • TheGlobalRegion
• GlobalVars,staticvars • Theheap–dynamicspace
User Data Memory
partitioned further for C: globals, heap, stack
x1FFF x2000
x3FFF x4000
Program Memory
Global Region
(static vars)
(dynamic vars)
(automatic vars)
Property of Penn Engineering 9
MCIT 593 – Introduction to Computer Systems
Character Array versus String versus Pointer to String
When you define a pointer to a string literal:
• The C compiler can place it anywhere it likes!
• It may be stored in static memory
• It may be in the OBJECT file itself (like immediate data)
• Editing a pointer to a string literal has undefined results
• On some compilers, it works! (for example, LCC)
• On other compilers, the data in the array is treated as read-only or “constant”
• You cannot edit a pointer to a string literal
char* my_string_ptr = “Tom” ;
my_string_ptr[0] = ‘M’ ; // not allowed! Literals are read-only
• You are perfectly allowed to edit a character array
char my_string [] = “Tom” ;
my_string[0] = ‘M’ ; // perfectly legal!
Property of Penn Engineering 10
MCIT 593 – Introduction to Computer Systems
FINDING THE LENGTH OF A STRING
Property of Penn Engineering 12
MCIT 593 – Introduction to Computer Systems
Standard C Library:
What is the Standard C Library?
• A library of functions that comes with all C compilers (except LCC) for you to use
• A nice reference for the entire Standard C Library
• http://www.tutorialspoint.com/c_standard_library/
• AspecificreferenceforthepartoftheStandardCLibrarythatdealswithstrings:
• https://www.tutorialspoint.com/c_standard_library/string_h.htm
• You get access to the “string” part of the library by placing:
#include
• It contains many functions folks want to do with strings, such as:
• find the length of a string: strlen()
• We will implement our own version of strlen() in this section
• This will give you an idea of how the string functions work in the Standard C library
• copy, join, compare strings…and many more are in the library!
Property of Penn Engineering 13
MCIT 593 – Introduction to Computer Systems
Why Do We Need NULLs at the End of Strings?
So we can find the end of them!
int strlen (char* string) {
int length = 0 ;
while (string[length] != ‘\0’) {
length++ ;
return length ;
x7FF3 R5 x7FF4
x7FF5 RV x7FF6 *string
x7FFB len x7FFC R5 x7FFD
int main() {
int len = 0 ;
my_string[0] x7FF8 my_string[1] x7FF9 my_string[2]
my_string[3]
char my_string [] = “Tom” ;
len = strlen (my_string) ;
Property of Penn Engineering 14
MCIT 593 – Introduction to Computer Systems
Why Do We Need NULLs at the End of Strings?
So we can find the end of them!
int strlen (char* string) {
int length = 0 ;
x7FF3 R5 x7FF4
x7FF6 *string
while (string[length] != ‘\0’) {
length++ ;
return length ;
my_string[0] x7FF8 my_string[1] x7FF9 my_string[2]
x7FFA my_string[3] x7FFB len
x7FFE x7FFF
int main() {
int len = 0 ;
char my_string [] = “Tom” ;
len = strlen (my_string) ;
Property of Penn Engineering 15
MCIT 593 – Introduction to Computer Systems
Why Do We Need NULLs at the End of Strings?
my_string[0]
Will the same code work on a literal?
int strlen (char* string) {
int length = 0 ;
x2001 x2003
my_string[3]
my_string[1] x2002 my_string[2]
while (string[length] != ‘\0’) {
length++ ;
return length ;
x7FF5 length
x7FF6 R5
x7FF8 RV
x7FF9 *string x7FFA *my_string
x7FFB len x7FFC R5 x7FFD
int main() {
int len = 0 ;
char* my_string = “Tom” ; // literal len = strlen (my_string) ;
This works perfectly, string is just located in a different region of memory
Property of Penn Engineering 16
MCIT 593 – Introduction to Computer Systems
IMPROVING – STRLEN()
Property of Penn Engineering 18
MCIT 593 – Introduction to Computer Systems
Our Implementation of strlen()
Inside strlen() we treated “string” as an array…we could treat it as the pointer it is!
int strlen (char string[]) {
int length = 0 ;
while (string[length] != ‘\0’)
length++ ;
return length ;
int main() {
int len = 0 ;
char my_string [] = “Tom” ;
len = strlen (my_string) ;
Property of Penn Engineering 19
MCIT 593 – Introduction to Computer Systems
An Implementation of strlen() Using Pointers
We can use pointer arithmetic instead:
int strlen (char* string) {
for (s = string; *s; s++) ;
return (s – string);
int main() {
int len = 0 ;
x7FF2 *s x7FF3 R5 x7FF4
x7FF5 RV x7FF6 *string
char my_string [] = “Tom” ;
len = strlen (my_string) ;
my_string[0] x7FF8 my_string[1] x7FF9 my_string[2]
x7FFA my_string[3] x7FFB len
x7FFC R5
x7FFE x7FFF
Property of Penn Engineering 20
MCIT 593 – Introduction to Computer Systems
An Implementation of strlen() Using Pointers
We can use pointer arithmetic instead:
int strlen (char* string) {
for (s = string; *s; s++) ;
return (s – string);
int main() {
int len = 0 ;
x7FF2 *s x7FF3 R5 x7FF4
x7FF5 RV x7FF6 *string
char my_string [] = “Tom” ;
len = strlen (my_string) ;
my_string[0] x7FF8 my_string[1] x7FF9 my_string[2]
x7FFA my_string[3] x7FFB len
x7FFE x7FFF
Property of Penn Engineering 21
MCIT 593 – Introduction to Computer Systems
An Implementation of strlen() Using Pointers
We can use pointer arithmetic instead:
int strlen (char* string) {
for (s = string; *s; s++) ;
return (s – string);
int main() {
int len = 0 ;
x7FF2 *s x7FF3 R5 x7FF4
x7FF5 RV x7FF6 *string
char my_string [] = “Tom” ;
len = strlen (my_string) ;
my_string[0] x7FF8 my_string[1] x7FF9 my_string[2]
x7FFA my_string[3] x7FFB len
x7FFE x7FFF
Property of Penn Engineering 22
MCIT 593 – Introduction to Computer Systems
An Implementation of strlen() Using Pointers
We can use pointer arithmetic instead:
int strlen (char* string) {
for (s = string; *s; s++) ;
return (s – string);
int main() {
int len = 0 ;
x7FF2 *s x7FF3 R5 x7FF4
x7FF5 RV x7FF6 *string
char my_string [] = “Tom” ;
len = strlen (my_string) ;
my_string[0] x7FF8 my_string[1] x7FF9 my_string[2]
x7FFA my_string[3] x7FFB len
x7FFE x7FFF
Property of Penn Engineering 23
MCIT 593 – Introduction to Computer Systems
An Implementation of strlen() Using Pointers
We can use pointer arithmetic instead:
int strlen (char* string) {
for (s = string; *s; s++) ;
return (s – string);
int main() {
int len = 0 ;
x7FF2 *s x7FF3 R5 x7FF4
x7FF5 RV x7FF6 *string
char my_string [] = “Tom” ;
len = strlen (my_string) ;
my_string[0] x7FF8 my_string[1] x7FF9 my_string[2]
x7FFA my_string[3] x7FFB len
x7FFE x7FFF
Property of Penn Engineering 24
MCIT 593 – Introduction to Computer Systems
An Implementation of strlen() Using Pointers
We can use pointer arithmetic instead:
int strlen (char* string) {
for (s = string; *s; s++) ;
return (s – string);
int main() {
int len = 0 ;
x7FF2 *s x7FF3 R5 x7FF4
x7FF5 RV x7FF6 *string
char my_string [] = “Tom” ;
len = strlen (my_string) ;
my_string[0] x7FF8 my_string[1] x7FF9 my_string[2]
x7FFA my_string[3] x7FFB len
x7FFC R5
x7FFE x7FFF
Property of Penn Engineering 25
MCIT 593 – Introduction to Computer Systems
An Implementation of strlen() Using Pointers
What would you predict?
int main() {
int len = 0 ;
char my_string [6] = “Tom” ;
len = strlen (my_string) ;
The strings length is 3
but we’ve allocated 6 rows on the stack!
strlen( ) only counts characters
up to (but not including) the first ‘\0’
my_string[0]
my_string[2]
x7FF6 my_string[1]
x7FF8 my_string[3]
x7FF9 my_string[4]
x7FFB len x7FFC
my_string[5]
Property of Penn Engineering 26
MCIT 593 – Introduction to Computer Systems
ARRAY OF STRINGS
Property of Penn Engineering 28
MCIT 593 – Introduction to Computer Systems
Arrays of Strings
In C you can have multidimensional arrays of any datatype
• You can take a 2D array of characters and make it an array of strings…
• …if each row of the array ends with a NULL character
• This is an array of strings in C:
• char my_2D_array [3][4] = { {‘I’, ‘\0’}, {‘a’, ‘m’, ‘\0’},
{‘T’, ‘o’, ‘m’,‘\0’} } ;
• This is just a 2D array of characters in C (notice…no NULLs):
• char my_2D_array [3][4] = { {‘I’}, {‘a’, ‘m’},
{‘T’, ‘o’, ‘m’} } ;
• You could use a function like strlen() on rows in the array of strings, but not in the array of characters
Property of Penn Engineering 29
MCIT 593 – Introduction to Computer Systems
Arrays of Strings
int main() {
char my_2D_array [3][4] = {
{‘I’, ‘\0’},
{‘a’, ‘m’, ‘\0’},
{‘T’, ‘o’, ‘m’,‘\0’}
Logically, the 2D array has 3 rows and 4 columns:
• You could also consider this 1D array of 3 strings Notice the waste in memory…
The Stack is certainly 1D, so in reality,
it is stored like this:
x7FF0 my_2D_array[0][0] x7FF1 my_2D_array[0][1]
my_2D_array[0][2]
x7FF3 my_2D_array[0][3]
x7FF4 my_2D_array[1][0]
x7FF5 my_2D_array[1][1]
x7FF6 my_2D_array[1][2]
x7FF7 my_2D_array[1][3]
x7FF8 my_2D_array[2][0]
x7FF9 my_2D_array[2][1]
x7FFC R5 x7FFD
Property of Penn Engineering 30
my_2D_array[2][2]
my_2D_array[2][3]
MCIT 593 – Introduction to Computer Systems
Passing a 2D Array in C Let’s say we wish to pass the 2D array to a function
int strlen_2D (char my_2D_ptr [3][4]) { int i=0, length=0 ;
for (i=0 ; i<3 ;i++)
length += strlen (my_2D_ptr [i]);
return (length) ;
int main() {
x7FE9 x7FEA
omitted RV,RA,FP
*my_2D_ptr
my_2D_array[0][0]
my_2D_array[0][1]
my_2D_array[0][2]
my_2D_array[0][3]
my_2D_array[1][0]
my_2D_array[1][1]
my_2D_array[1][2]
my_2D_array[1][3]
my_2D_array[2][0]
my_2D_array[2][1]
my_2D_array[2][2]
my_2D_array[2][3]
int len = 0 ;
char my_2D_array [3][4] = {
{‘I’, ‘\0’},
{‘a’, ‘m’, ‘\0’},
{‘T’, ‘o’, ‘m’,‘\0’}
len = strlen_2D (my_2D_array) ;
What do you think len will be after this runs?
Property of Penn Engineering 31
MCIT 593 - Introduction to Computer Systems
Passing a 2D Array in C This is also permittable in C
int strlen_2D (char my_2D_ptr [ ][4]) { int i=0, length=0 ;
for (i=0 ; i<3 ;i++)
length += strlen (my_2D_ptr [i]);
return (length) ;
int main() {
x7FE9 x7FEA
omitted RV,RA,FP
*my_2D_ptr
my_2D_array[0][0]
my_2D_array[0][1]
my_2D_array[0][2]
my_2D_array[0][3]
my_2D_array[1][0]
my_2D_array[1][1]
my_2D_array[1][2]
my_2D_array[1][3]
my_2D_array[2][0]
my_2D_array[2][1]
my_2D_array[2][2]
my_2D_array[2][3]
int len = 0 ;
char my_2D_array [3][4] = {
{‘I’, ‘\0’},
{‘a’, ‘m’, ‘\0’},
{‘T’, ‘o’, ‘m’,‘\0’}
len = strlen_2D (my_2D_array) ;
Property of Penn Engineering 32
MCIT 593 - Introduction to Computer Systems
Passing a 2D Array in C This is NOT permittable in C!
int strlen_2D (char my_2D_ptr [ ][ ]) { int i=0, length=0 ;
for (i=0 ; i<3 ;i++)
length += strlen (my_2D_ptr [i]);
return (length) ;
int main() {
x7FE9 x7FEA
omitted RV,RA,FP
*my_2D_ptr
my_2D_array[0][0]
my_2D_array[0][1]
my_2D_array[0][2]
my_2D_array[0][3]
my_2D_array[1][0]
my_2D_array[1][1]
my_2D_array[1][2]
my_2D_array[1][3]
my_2D_array[2][0]
my_2D_array[2][1]
my_2D_array[2][2]
my_2D_array[2][3]
int len = 0 ;
char my_2D_array [3][4] = {
{‘I’, ‘\0’},
{‘a’, ‘m’, ‘\0’},
{‘T’, ‘o’, ‘m’,‘\0’}
len = strlen_2D (my_2D_array) ;
Property of Penn Engineering 33
MCIT 593 - Introduction to Computer Systems
Passing a 2D Array in C
In reality, are function is receiving a memory address:
int strlen_2D (char (*my_2D_ptr)[4]) { int i=0, length=0 ;
for (i=0 ; i<3 ;i++)
length += strlen (my_2D_ptr [i]);
return (length) ;
int main() {
x7FE9 x7FEA
omitted RV,RA,FP
*my_2D_ptr
my_2D_array[0][0]
my_2D_array[0][1]
my_2D_array[0][2]
my_2D_array[0][3]
my_2D_array[1][0]
my_2D_array[1][1]
my_2D_array[1][2]
my_2D_array[1][3]
my_2D_array[2][0]
my_2D_array[2][1]
my_2D_array[2][2]
my_2D_array[2][3]
int len = 0 ;
char my_2D_array [3][4] = {
{‘I’, ‘\0’},
{‘a’, ‘m’, ‘\0’},
{‘T’, ‘o’, ‘m’,‘\0’}
len = strlen_2D (my_2D_array) ;
Property of Penn Engineering 34
MCIT 593 - Introduction to Computer Systems
Passing a 2D Array in C Dereferencing with [ ] is automated pointer arithmetic
int strlen_2D (char (*my_2D_ptr)[4]) { int i=0, length=0 ;
x7FE9 x7FEA
omitted RV,RA,FP
*my_2D_ptr
my_2D_array[0][0]
my_2D_array[0][1]
my_2D_array[0][2]
my_2D_array[0][3]
my_2D_array[1][0]
my_2D_array[1][1]
my_2D_array[1][2]
my_2D_array[1][3]
my_2D_array[2][0]
my_2D_array[2][1]
my_2D_array[2][2]
my_2D_array[2][3]
for (i=0 ; i<3 ;i++)
length += strlen (my_2D_ptr [i]);
return (length) ;
So when we deference the pointer with:
my_2D_ptr[0]
We are actually pointing to the 1st address of the 1st string
Property of Penn Engineering 35
MCIT 593 - Introduction to Computer Systems
Passing a 2D Array in C Dereferencing with [ ] is automated pointer arithmetic
int strlen_2D (char (*my_2D_ptr)[4]) { int i=0, length=0 ;
x7FE9 x7FEA
omitted RV,RA,FP
*my_2D_ptr
my_2D_array[0][0]
my_2D_array[0][1]
my_2D_array[0][2]
my_2D_array[0][3]
my_2D_array[1][0]
my_2D_array[1][1]
my_2D_array[1][2]
my_2D_array[1][3]
my_2D_array[2][0]
my_2D_array[2][1]
my_2D_array[2][2]
my_2D_array[2][3]
for (i=0 ; i<3 ;i++)
length += strlen (my_2D_ptr [i]);
return (length) ;
And when we increase that by 1
my_2d_ptr[1]
We are actually pointing to the 1st address of the 2nd string
Property of Penn Engineering 36
MCIT 593 - Introduction to Computer Systems
Passing a 2D Array in C Dereferencing with [ ] is automated pointer arithmetic
int strlen_2D (char (*my_2D_ptr)[4]) { int i=0, length=0 ;
x7FE9 x7FEA
omitted RV,RA,FP
*my_2D_ptr
my_2D_array[0][0]
my_2D_array[0][1]
my_2D_array[0][2]
my_2D_array[0][3]
my_2D_array[1][0]
my_2D_array[1][1]
my_2D_array[1][2]
my_2D_array[1][
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com