ARRAYS, POINTERS, AND STRINGS - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

ARRAYS, POINTERS, AND STRINGS

Description:

1. ARRAYS, POINTERS, AND STRINGS. CHAPTER 6 (PART 2: arrays, ... Ragged Array. An array of pointers whose elements are used to point to arrays of varying sizes ... – PowerPoint PPT presentation

Number of Views:43
Avg rating:3.0/5.0
Slides: 19
Provided by: SEN74
Category:

less

Transcript and Presenter's Notes

Title: ARRAYS, POINTERS, AND STRINGS


1
ARRAYS, POINTERS, AND STRINGS
  • CHAPTER 6
  • (PART 2 arrays, pointers, strings, and sorting)
  • prepared by Senem Kumova Metin
  • modified by Ilker Korkmaz

2
A brief review on array
  • int grade0, grade1, grade2
  • int grade 3
  • Arrays represent a group of homogenous typed
    values.

grade0
grade1
grade2
grade0
grade1
grade2
3
Multidimensional Arrays
  • int y23 // two-dimensional
  • int z 273 // three-dimensional
  • int a223
  • 1, 1, 0, 2, 0, 0 ,
  • 3, 0, 0, 4, 4, 0

4
Array Sorting Example BUBBLE SORT
  • void swap(int , int )  / swap was defined
    before /
  • void bubble(int a , int n)  / n is the size of
    a /   int  i, j   for (i 0 i lt n - 1
    i)        for (j n -1 j gt i --j)
                if (aj-1 gt aj)                
    swap(aj-1, aj)
  • / bubble sort algorithm has a worst-case
    complexity of O(n2) /

5
Another Sorting Example MERGE SORT
  • The merge sort algorithm splits the list to be
    sorted into two equal halves, and places them in
    separate arrays. Each array is recursively
    sorted, and then merged back together to form the
    final sorted list. Like many recursive sort
    algotihms, the merge sort has an algorithmic
    complexity of O(n log n).
  • TO DO merge and mergesort functions
    available on page 263 of the textbook.

6
STRINGS
  • Strings are one-dimensional special arrays of
    type char
  • A string in C is terminated by the end-of-string
    sentinel \0, or null character. The null
    characters decimal value is zero.
  • char sample_st1 a,b,c,\0
  • char sample_st2 abc
  • String constants are written between double
    quotes, e.g., "abc", "" (an empty string
    containing null character \0)
  • " a " ? a string with 2 elements (a and \0)
  • a ? a char constant with only 1 element
  • " " ? a string with only 1 element (\0)

7
String Handling Functions (1)
  • requires
  • include ltstring.hgt
  • char strcat(char s1, const char s2) 
  • takes two strings as arguments, concatenates
    them, puts the results in s1, and returns s1.
  • char d1abc char d2def
  • strcat(d1,d2) // What is the result d1?????
    d2?????
  • int strcmp(const char s1, const char s2)
  • two strings are compared. 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.

8
String Handling Functions (2)
  • char strcpy(char s1, const char s2)
  • the characters in the string s2 are copied into
    string s1 until \0 is moved, and the pointer to
    s1 is returned. Whatever exists in s1 is
    overwritten. It is assumed that s1 has enough
    space to hold the result.
  • Can you define the implementation for strcpy
    ?
  • size_t strlen(const char s)
  • a count of the number of characters before \0 is
    returned. ANSI C requires the type size_t to be
    an integral unsigned type.
  • You should be able to implement strlen function

9
String Handling Functions (3)
  • char d1 ball hall
  • char d2 car toy
  • strlen(d1) ? 9
  • strcmp(d1,d2) ? negative
  • printf(s,d15) ?hall
  • strcpy(d15,d24) ?hall toy ( is it true?)
  • strcat(d1, s!!) ?hall toys!! ( is it true?)
  • Note More is available on page 272.

10
A review on the use of typedef
  • define N 4
  • typedef int scalar
  • typedef scalar vectorN
  • typedef scalar matrixNN
  • // What are globally defined above?

11
Arrays of Pointers
  • / a word sorting example, which is available on
    page 284. /
  • includeltstdio.gt
  • includeltstring.hgt
  • define N 5
  • main()
  • char word100 char xN
  • for(i0 iltN i)
  • scanf(s,word) // get the word from the
    user
  • xi(char ) malloc((strlen(word)1)sizeof(
    char))
  • strcpy(xi,word) // put the corresponding
    word into the array
  • for(i0 iltN i)
  • printf(the word in xds\n,i,xi)

12
Sorting the Strings ?TO DO pg 284 -290
  • void swap(char p, char q)
  • char tmp
  • tmp p
  • p q
  • q tmp
  • main()
  • .
  • char w5
  • ........
  • swap(w1, w4)
  • .

13
Arguments to main() (1)
  • Two arguments, conventionally called argc and
    argv, can be used with main() to communicate with
    the operating system.
  • The variable argc gt 1 provides a count to the
    number of command line arguments, including the
    programs name itself.
  • The array argv is an array of pointers, each
    pointer pointing to a string - a component of the
    command line.
  • main( int argc, char argv)

14
Arguments to main() (2)
  • / echoing the command line arguments./
  • include ltstdio.hgt int main(int argc, char
    argv)     int i    printf("argc d\n",
    argc)    for (i 0 i lt argc i)       
    printf("argvd s\n", i, argvi)    return
    0 
  • / try to run the program with some arguments at
    the command line /

15
Ragged Array
  • An array of pointers whose elements are used to
    point to arrays of varying sizes
  • char a215 "abc", "a is for apple"
  • char p2 "abc", "a is for apple"
  • // a allocates (215) bytes in memory.
  • // p needs only (515) bytes.
  • // p does its work in less space than a.

16
Functions as Arguments (1)
  • In C, a function name by itself is treated by the
    compiler as a pointer to the function, which is
    analogous to the idea that an array name by
    itself is treated as a pointer to the base of the
    array in memory
  • f            the pointer to a function
  • f           the function itself (after
    dereferencing)
  • (f)(x)    the call to the function (equivalently
    f(x))

17
Functions as Arguments (2)
  • include ltmath.hgt    include ltstdio.hgt   
    double  f(double x)
  •     double  sum_square(double f(double x), int
    m, int n)    int main(void)   
     printf("f\nf\n", sum_square(f, 1, 10000),
    sum_square(sin, 2, 13))     return 0   
    double sum_square(double f(double x), int m, int
    n)      / OR double sum_square(double
    (f)(double), int m, int n)                  /
              int     k       double  sum
    0.0       for (k m k lt n k)           
    sum f(k) f(k)       / equivalently can be
    written as sum (f)(k) (f)(k) /      
    return sum        double f(double x)   /
    function f declaration /          return 1.0
    / x   

18
The Type Qualifiers const and volatile
  • They restrict, or qualify, the way an identifier
    of a given type can be used.
  • const comes after the storage class (if any),
    but before the type, means that the variable can
    be initialized, but thereafter the variable
    cannot be assigned to or modified.
  • static const int k 3
  • volatile object is one that can be modified in
    some unspecified way by the hardware.
  • extern const volatile int real_time_clock
  • The extern means look for real_time_clock either
    in this file or in some other file.
  • The volatile qualifier indicates that the object
    may be acted on by the hardware because it is
    const, it cannot be modified by the program,
    however the hardware can change the clock.
Write a Comment
User Comments (0)
About PowerShow.com