Title: Arrays, Pointers, Strings
1Arrays, Pointers, Strings
2Pointer arithmetic and element size
- double a2, p, q
- pa /points to base of array/
- qp1 / ? qa1 /
- printf(d\n, q-p) / 1 printed /
- printf(d\n, (int)q - (int)p) / 8 printed /
3Arrays as function arguments
- In a function definition, a formal parameter that
is declared as an array is actually a pointer. - When an array is passed as an argument to a
function, the base address of the array is passed
call by value. The array elements are not
copied.
double sum (double a, int n) int i
double sum for (i0,sum0.0 iltn i)
sum ai return sum
double sum (double a, int n) int i
double sum for (i0,sum0.0 iltn i)
sum ai return sum
4Recursively finding the sum
double sum (int a, int n) if (
) return return
0
n 0
a0
sum (a1, n-1)
5Various ways that sum() might be called
What gets computed and returned
Invocation
6Recursive programs using arrays
- int sum (int a, int size)
- if (size0) return 0
- return a0sum(a1,size-1)
void reverse (int a, int size) if (size0
size1) return swap2 (a0,
asize-1) reverse (a1,size-2)
could have written swap2(a,asize-1)
7Recursive programs using arrays
- int binsearch (int elem, int a, int size)
- if (size0) return NULL
- mid size/2
- if (elem amid) return amid
- if (elem gt amid)
- return binsearch (elem, amid1, size-mid-1)
-
- else return (elem, a, mid)
8Recursive programs using arrays
- void remove (char c, char s)
- char p
- if (s0\0) return
- if (s0 c)
- shiftleft (s)
- remove (c, s)
-
- else
- remove (c, s1)
void shiftleft (char s) int i
for (i0 si !\0i) si
si1
9Strings
- 1-d arrays of type char
- By convention, a string in C is terminated by the
end-of-string sentinel \0 (null character) - char s21 - can have variable length delimited
with \0. - Max length is 20 as the size must include storage
needed for the delimiter. - String constants hello, abc
- abc is a character array of size 4.
10Strings
- A string constant is treated as a pointer. Its
value is the base address of the string. - char p abc
- printf (s s\n,p,p1) / abc bc is printed /
p
a
b
c
\0
11Differences array pointers
- char p abcde
- The compiler allocates space for p, puts the
string constant abcde in memory somewhere
elese, initializes p with the base address of the
string constant.
- char s abcde
- ? char s a,b,c,d,e.\0
- The compiler allocates 6 bytes of memory for the
array s which are initialized with the 6
characters.
p
s
a
b
c
d
e
\0
a
b
c
d
e
\0
12A program using strings
/ Count the number of words in a string
/ include ltctype.hgt int word_cnt (char s)
int cnt 0, i for (i0 si !
\0) while (isspace(si))
/ skip white space /
i if (si ! \0)
/ found a word /
cnt while
(!isspace(si) si!\0) / skip the word
/ i return cnt
13The program with pointers
/ Count the number of words in a string
/ include ltctype.hgt int word_cnt (char s)
int cnt 0 while (s ! \0)
while (isspace(s))
/ skip white space / s
if (s ! \0)
/ found a word /
cnt while (!isspace(s) s!\0)
/ skip the word / s
return cnt
14String-handling functions in the standard library
- define string.h
- char strcat (char s1, const char s2)
- Takes 2 strings as arguments, concatenates them,
and puts the result in s1. Returns s1. Programmer
must ensure that s1 points to enough space to
hold the result.
char strcat(char s1, const char s2)
char p s1 while (p) / go to the end
/ p while (p s2) / copy
/ return s1
15Dissection of the strcat() function
- char p s1
- p is being initialized, not p. The pointer p is
initialized to the pointer value s1. Thus p and
s1 point to the same memory location. - while (p) p / ? while (p ! \0)
p / - As long as the value pointed to by p is non-zero,
p is incremented, causing it to point at the next
character in the string. When p points to \0, the
expression p has the value 0, and control exits
the while statement. - while (p s2)
- At the beginning, p points to the null character
at the end of string s1. The characters in s2 get
copied one after another.
16- int strcmp (const char s1, const char s2)
Two strings are passed as arguments. An integer
is returned that is less than, equal to, or
greater than 0, depending on whether s1 is
lexicographically less than, equal to, or greater
than s2.
int strcmp(char s1, char s2) for
(s1!\0s2!\0 s1,s2) if
(s1gts2) return 1 if (s2gts1) return -1
if (s1 ! \0) return 1 if (s2
! \0) return -1 return 0
17- char strcpy (char s1, const char s2)
- The characters is the string s2 are copied into
s1 until \0 is moved. Whatever exists in s1 is
overwritten. It is assumed that s1 has enough
space to hold the result. The pointer s1 is
returned. - size_t strlen (const char s)
- A count of the number of characters before \0 is
returned.
size_t strlen (const char s) size_t n
for (n0 s!\0 s) n return
n
char strcpy (char s1, char s2) char
p s1 while (p s2)
return s1
18Examples of string handling functions
- char s1 beautiful big sky country,
- s2 how now brown cow
- Expression Value
- strlen (s1)
- strlen (s28)
- strcmp(s1,s2)
- Statements What gets printed
- printf(s,s110)
- strcpy(s110,s28)
- strcat(s1, s!)
- printf(s, s1)
19Examples of string handling functions
- char s1 beautiful big sky country,
- s2 how now brown cow
- Expression Value
- strlen (s1) 25
- strlen (s28) 9
- strcmp(s1,s2) negative integer
- Statements What gets printed
- printf(s,s110) big sky country
- strcpy(s110,s28)
- strcat(s1, s!)
- printf(s, s1) beautiful brown
cows!