Arrays, Pointers and Strings - PowerPoint PPT Presentation

1 / 44
About This Presentation
Title:

Arrays, Pointers and Strings

Description:

Arrays, Pointers and Strings – PowerPoint PPT presentation

Number of Views:23
Avg rating:3.0/5.0
Slides: 45
Provided by: Ala774
Category:
Tags: arrays | do | hebrew | how | in | my | name | pointers | strings | write

less

Transcript and Presenter's Notes

Title: Arrays, Pointers and Strings


1
Arrays, Pointers and Strings
2
One Dimensional Arrays
  • define N 100
  • int aN
  • for ( i 0 ilt N i )
  • sum ai

space for a0, ..., a99 is allocated
process element a i
3
One Dimensional Arrays
  • float f5 0.0, 1.0, 2.0, 3.0, 4.0
  • int a100 0
  • int a 2, 3, 5, -7
  • char s abc
  • char s a, b, c, \0

4
Pointers
  • The nightmare of C students

5
Pointers
  • p 0
  • p NULL
  • p i

equivalent to p 0
6
Pointers
  • int a 1, b 2, p
  • p a
  • b p is equivalent to b a

7
Pointers
  • int a 1, b 2, p
  • p a
  • b p is equivalent to b a
  • This is also correct
  • int a 1, b 2, p a

8
Pointers
  • include ltstdio.hgt
  • int main(void)
  • int i 7, p
  • p i
  • printf( sd\nsu\n, Value of i , p,
    Location of i , p )
  • return 0
  • Value of i 7
  • Location of i 251657504

The result of the printing
9
Pointers - Illustration
memory

num
int num
5
int p
p
p num
arr
p 5
int arr5
9
(arr 3) 9
pp
int pp
pp p
10
Declarations and Initializations
  • int i 3, j 5, p i, q j, r
  • double x

11
Declarations and Initializations
  • Declaration
  • int p
  • float q
  • void v
  • More illegal
  • 3,
  • (k 99),
  • v

12
Call by Refernce
  • include ltstdio.hgt
  • void swap(int p, int q)
  • int tmp
  • tmp p
  • p q
  • q tmp
  • int main(void)
  • int i 3, j 5
  • swap(i, j)
  • printf( d d\n, i, j )
  • return 0

5 3 is printed
13
Arrays and Pointers
Declaration int p int aN
  • ai is equivalent to (a i)
  • pi is equivalent to (p i)
  • p a is equivalent to p a0
  • p a 1 ? is equivalent to p a1

An array name is an address!
Pointer arithmetic array indexing
14
Arrays and Pointers
  • for ( p a p lt a N p )
  • sum p
  • is equivalent to
  • for ( i 0 i lt N i )
  • sum ( a i )
  • is equivalent to
  • p a
  • for ( i 0 ilt N i )
  • sum p i

Declaration int p int aN
Illegal expressions
a p a a 2 a
15
Pointer Arithmetic and Element Size
  • double a2, p NULL, q NULL
  • p a
  • q p 1
  • printf(d\n, q - p)
  • printf(d\n, (int)q - (int)p)

points to the base the array
equivalent to q a 1
1 is printed
8 is printed
16
Arrays as Function Arguments
  • double sum(double a, int n)
  • int i 0
  • double sum 0.0
  • for ( i 0 i lt n i )
  • sum ai
  • return sum

n is the size of a
17
An Example Bubble Sort
  • void bubble( int a, int n )
  • int i 0, j 0
  • void swap( int , int )
  • for ( i 0 i lt n - 1 i )
  • for ( j n - 1 j gt i --j )
  • if ( a j-1 gt a j )
  • swap( a j-1, a j )

n is the size of a
18
Memory Allocation
19
Memory Allocation
  • include ltstdlib.hgt
  • void malloc(size_t size)
  • malloc() allocates size bytes of memory.
  • Upon successful completion with size not equal
    to 0, malloc() shall return a pointer to the
    allocated space. If size is 0, either a null
    pointer or a unique pointer that can be
    successfully passed to free() shall be returned.
    Otherwise, it shall return a null pointer.

20
Memory Allocation
  • include ltstdlib.hgt
  • void calloc(size_t nelem, size_t elsize)
  • calloc() allocates nelem elements of elsize
    bytes each.
  • Upon successful completion with both nelem and
    elsize non-zero, calloc() returns a pointer to
    the allocated space. If either nelem or elsize is
    0, then either a null pointer or a unique pointer
    value that can be successfully passed to free()
    shall be returned. Otherwise, it shall return a
    null pointer.

21
Memory Allocation
  • include ltstdlib.hgt
  • void realloc(void ptr, size_t size)
  • The realloc() function changes the size of the
    memory pointed to by ptr to the size specified by
    size. The contents remain unchanged up to the
    lesser of the new and old sizes. If the new size
    of the memory object requires movement of the
    object, the space for the previous instantiation
    of the object is freed.
  • Upon successful completion with a size not equal
    to 0, realloc() shall return a pointer to the
    (possibly moved) allocated space. If size is 0,
    either a null pointer or a unique pointer that
    can be successfully passed to free() shall be
    returned. If there is not enough available
    memory, realloc() shall return a null pointer

22
Memory Allocation
  • include ltstdlib.hgt
  • void free(void ptr)
  • The free() function causes the space pointed to
    by ptr to be deallocated.
  • If the argument does not match a pointer earlier
    returned by the calloc, malloc or realloac, or if
    the space has been deallocated by a call to
    free() the behavior is undefined.
  • Any use of a pointer that refers to freed space
    results in undefined behavior.

23
Merge Sort Example
24
Merge (and sort)
  • void merge(int a, int b, int c, int m, int
    n)
  • int i 0, j 0, k 0
  • while ( i lt m j lt n )
  • if ( ai lt bj )
  • ck ai
  • else
  • ck bj
  • while ( i lt m )
  • ck ai
  • while (j lt n)
  • ck bj

Merge a of size m and b of size n into
c. ASSUMPTION c is big enough to store both a
and b.
pickup any remainder
25
Mergesort
  • include ltstdio.hgt
  • include ltstdlib.hgt
  • void merge(int , int , int , int, int)
  • void mergesort(int key, int n)
  • int j 0, k 0, m 0, w NULL
  • for ( m 1 m lt n m 2 )
  • if ( m ! n )
  • printf( ERROR Size of the array is
    not a power of 2 - bye!\n )
  • exit( 1 )

Use merge() to sort an array of size n
no additional statements for this loop
26
Mergesort
allocate workspace
  • w (int)calloc( n, sizeof( int ) )
  • for ( k 1 k lt n k 2 )
  • for ( j 0 j lt n - k j 2 k )
  • merge( key j, key j k, w j, k, k )
  • for (j 0 j lt n j)
  • keyj wj
  • free(w)
  • w NULL

merge into w
write w back into key
free the workspace
27
Mergesort
Key j, Key j k
W j (copy 2 k values)
28
Mergesort
  • include ltstdio.hgt
  • define KEYSIZE 16
  • void mergesort(int , int)
  • int main(void)
  • int i, key 4, 3, 1, 67, 55, 8, 0, 4, -5,
    37, 7, 4, 2, 9, 1, -1
  • mergesort(key, KEYSIZE)
  • printf( After mergesort\n )
  • for ( i 0 i lt KEYSIZE i )
  • printf( 4d, keyi )
  • putchar( \n )
  • return 0

29
Strings
30
Strings
  • char p abcde
  • printf( s s\n, p, p 1 )
  • char s abcde
  • is equivalent to
  • char s a, b, c, d, e, \0

abcde bcde is printed
31
Strings
  • char p abcde vs. char s abcde

p
s
32
Strings
  • abc1 and (abc 2) make sense
  • char s NULL
  • int nfrogs 0
  • . . . .
  • s (nfrogs 1)? s
  • printf( we found d frogs in the pond!\n,
    nfrogs, s )

NULL Strings
33
Count the number ofwords in a string
  • include ltctype.hgt
  • int word_cnt(char s)
  • int cnt 0
  • while (s ! '\0')
  • while (isspace(s))
  • s
  • if (s ! '\0')
  • cnt
  • while (!isspace(s) s ! '\0')
  • s
  • return cnt

skip white spaces
found a word
skip the word
34
String Handling Functions in the Standard Library
  • char strcat(char s1, const char s2)
  • int strcmp(const char s1, const char s2)
  • char strcpy(char s1, const char s2)
  • unsigned strlen(const char s)

35
String Handling Functions in the Standard Library
  • unsigned strlen( const char s )
  • register int n 0
  • for ( n 0 s ! '\0' s )
  • n
  • return n

36
String Handling Functions in the Standard Library
  • char strcat( char s1, const char s2 )
  • register char p s1
  • while ( p )
  • p
  • while ( p s2 )
  • return s1

no more statements in this loop
37
Multidimensional Arrays
  • int sum( int a5 )
  • int i 0, j 0, sum 0
  • for ( i 0 i lt 3 i )
  • for ( j 0 j lt 5 j )
  • sum aij
  • return sum

38
Multidimensional Arrays
  • int sum( int a92 )
  • int i 0, j 0, k 0, sum 0
  • for ( i 0 i lt 7 i )
  • for ( j 0 j lt 9 j )
  • for ( k 0 k lt 2 k )
  • sum aijk
  • return sum

39
Sort Words Lexicographically
  • include ltstdio.hgt
  • include ltstdlib.hgt
  • include ltstring.hgt
  • define MAXWORD 50 / max word size
    /
  • define N 1000 / array size /
  • void sort_words(char , int)
  • void swap(char , char )

40
Sort Words Lexicographically
  • int main(void)
  • char wN
  • char wordMAXWORD
  • int n 0, i 0
  • for ( i 0 scanf("s", word) 1 i )
  • if ( i gt N )
  • printf( Sorry, at most d words can be
    sorted., N )
  • exit(1)
  • wi (char)calloc(strlen(word) 1,
    sizeof(char))
  • strcpy(wi, word)

an array of pointers
work space
number of words to be stored
41
Sort Words Lexicographically
  • n i
  • sort_words( w, n )
  • for ( i 0 i lt n i )
  • printf( s\n, wi )
  • return 0

print the sorted words
42
Sort Words Lexicographically
  • void sort_words( char w, int n )
  • int i 0, j 0
  • for ( i 0 i lt n i )
  • for ( j i 1 j lt n j )
  • if ( strcmp(wi, wj) gt 0 )
  • swap( wi, wj )
  • void swap( char p, char q )
  • char temp NULL
  • temp p
  • p q
  • q temp

n elements to be sorted
43
Arguments to main()
  • include ltstdio.hgt
  • void main(int argc, char argv)
  • int i 0
  • printf( argc d\n, argc )
  • for ( i 0 i lt argc i )
  • printf( argvd s\n, i, argvi )

44
Functions as Arguments
  • double sum_square( double f(double), int m, int n
    )
  • int k 0
  • double sum 0.0
  • for ( k m k lt n k )
  • sum f(k) f(k)
  • return sum
  • double sum_square( double (f)(double), int m,
    int n )
  • .....

This slide is for general knowledge only!
Write a Comment
User Comments (0)
About PowerShow.com