Arrays, Pointers, Strings - PowerPoint PPT Presentation

About This Presentation
Title:

Arrays, Pointers, Strings

Description:

... strings as arguments, concatenates them, and puts the result in s1. ... char s1[] = 'beautiful big sky country', s2[] = 'how now brown cow'; Expression Value ... – PowerPoint PPT presentation

Number of Views:390
Avg rating:3.0/5.0
Slides: 20
Provided by: roychow
Category:

less

Transcript and Presenter's Notes

Title: Arrays, Pointers, Strings


1
Arrays, Pointers, Strings
  • Lecture 18
  • 19/2/2002

2
Pointer 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 /

3
Arrays 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
4
Recursively finding the sum
double sum (int a, int n) if (
) return return

0
n 0
a0
sum (a1, n-1)
5
Various ways that sum() might be called
What gets computed and returned
Invocation
6
Recursive 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)
7
Recursive 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)

8
Recursive 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
9
Strings
  • 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.

10
Strings
  • 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
11
Differences 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
12
A 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
13
The 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
14
String-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
15
Dissection 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
18
Examples 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)

19
Examples 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!
Write a Comment
User Comments (0)
About PowerShow.com