home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS 1992 September / Simtel20_Sept92.cdr / msdos / turbo_c / trboctxt.arc / CHAP7.TXT < prev    next >
Text File  |  1988-02-01  |  19KB  |  455 lines

  1.  
  2.                       Chapter 7 - Strings and Arrays
  3.  
  4.  
  5.                              WHAT IS A STRING?
  6.  
  7.              A  string is a group of characters,  usually letters of
  8.         the  alphabet.   In order to format your printout in such  a
  9.         way that it looks nice, has meaningful titles and names, and
  10.         is  esthetically  pleasing to you and the people  using  the
  11.         output of your program,  you need the ability to output text
  12.         data.  Actually you have already been using strings, because
  13.         the second program in this tutorial,  way back in Chapter 2,
  14.         output a message that was handled internally as a string.  A
  15.         complete  definition  is  a  series  of  "char"  type   data
  16.         terminated by a NULL character, which is a zero.
  17.  
  18.              When  C  is going to use a string of data in some  way,
  19.         either  to compare it with another,  output it,  copy it  to
  20.         another string,  or whatever, the functions are set up to do
  21.         what they are called to do until a NULL, which is a zero, is
  22.         detected.
  23.  
  24.                              WHAT IS AN ARRAY?
  25.  
  26.              An array is a series of homogeneous pieces of data that
  27.         are all identical in type, but the type can be quite complex
  28.         as  we will see when we get to the chapter of this  tutorial
  29.         discussing structures.  A string is simply a special case of
  30.         an array, a series of char type data.
  31.  
  32.              The  best way to see these principles is by use  of  an
  33.         example,  so  load  the program CHRSTRG.C and display it  on
  34.         your monitor.   The first thing new is the line that defines
  35.         a "char" type of data entity.  The square brackets define an
  36.         array subscript in C, and in the case of the data definition
  37.         statement,  the  5 in the brackets defines 5 data fields  of
  38.         type  "char" all defined as the variable "name".   In the  C
  39.         language,  all subscripts start at 0 and increase by 1  each
  40.         step  up  to  the  maximum which in  this  case  is  4.   We
  41.         therefore  have  5 "char" type variables  named,  "name[0]",
  42.         "name[1]",  "name[2]",  "name[3]",  and "name[4]".  You must
  43.         keep in mind that in C, the subscripts actually go from 0 to
  44.         one   less  than  the  number  defined  in  the   definition
  45.         statement.  This is due to the original definition of C  and
  46.         these   limits  cannot  be  changed  or  redefined  by   the
  47.         programmer.
  48.  
  49.                          HOW DO WE USE THE STRING?
  50.  
  51.              The  variable  "name" is therefore a string  which  can
  52.         hold up to 5 characters, but since we need room for the NULL
  53.         terminating  character, there are actually only four  useful
  54.         characters.   To load something useful into the  string,  we
  55.         have  5 statements, each of which assigns  one  alphabetical
  56.  
  57.  
  58.                                   Page 46
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.                       Chapter 7 - Strings and Arrays
  69.  
  70.  
  71.         character  to  one of the string characters.   Finally,  the
  72.         last place in the string is filled with the numeral 0 as the
  73.         end indicator and the string is complete.  (A "define" would
  74.         allow us to use "NULL" instead of a zero, and this would add
  75.         greatly  to  the clarity of the program. It  would  be  very
  76.         obvious that this was a NULL and not simply a zero for  some
  77.         other purpose.) Now that we have the string, we will  simply
  78.         print  it  out  with some other string data  in  the  output
  79.         statement.
  80.  
  81.              The %s is the output definition to output a string  and
  82.         the  system  will output characters starting with the  first
  83.         one in "name" until it comes to the NULL character,  and  it
  84.         will quit.   Notice that in the "printf" statement, only the
  85.         variable  name "name" needs to be given,  with no  subscript
  86.         since  we  are  interested  in starting  at  the  beginning.
  87.         (There  is  actually another reason that only  the  variable
  88.         name  is  given without brackets.   The discussion  of  that
  89.         topic will be given in the next chapter.)
  90.  
  91.                         OUTPUTTING PART OF A STRING
  92.  
  93.              The  next "printf" illustrates that we can  output  any
  94.         single  character of the string by using the "%c" and naming
  95.         the particular character of "name" we want by including  the
  96.         subscript.   The last "printf" illustrates how we can output
  97.         part  of the string by stating the starting point by using a
  98.         subscript.   The & specifies the address of  "name[1]".   We
  99.         will  study this in the next chapter but I thought you would
  100.         benefit from a little glimpse ahead.
  101.  
  102.              This example may make you feel that strings are  rather
  103.         cumbersome  to  use since you have to set up each  character
  104.         one  at  a time.   That is an incorrect  conclusion  because
  105.         strings  are  very easy to use as we will see  in  the  next
  106.         example program.
  107.  
  108.              Compile and run this program.
  109.  
  110.                           SOME STRING SUBROUTINES
  111.  
  112.              Load  the  example program STRINGS.C for an example  of
  113.         some  ways  to use strings.  First we define  four  strings.
  114.         Next  we  come  to a new function that you  will  find  very
  115.         useful,  the "strcpy" function,  or string copy.   It copies
  116.         from  one  string  to another until it  comes  to  the  NULL
  117.         character.  Remember that the NULL is actually a "0" and  is
  118.         added to the character string by the system.  It is easy  to
  119.         remember which one gets copied to which if you think of them
  120.         like an assignment statement.  Thus if you were to say,  for
  121.  
  122.  
  123.  
  124.                                   Page 47
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.                       Chapter 7 - Strings and Arrays
  135.  
  136.  
  137.         example, "x = 23;", the data is copied from the right entity
  138.         to the left one.  In the "strcpy" function, the data is also
  139.         copied  from  the right entity to the left,  so  that  after
  140.         execution  of  the first statement, name1 will  contain  the
  141.         string "Rosalinda", but without the double quotes, they  are
  142.         the  compiler's  way  of knowing that  you  are  defining  a
  143.         string.
  144.  
  145.              Likewise,  "Zeke"  is copied into "name2" by the second
  146.         statement,  then the "title" is copied.   The title and both
  147.         names are then printed out.   Note that it is not  necessary
  148.         for  the  defined string to be exactly the same size as  the
  149.         string it will be called upon to store,  only that it is  at
  150.         least  as long as the string plus one more character for the
  151.         NULL.
  152.  
  153.                       ALPHABETICAL SORTING OF STRINGS
  154.  
  155.              The  next function we will look at is the  "strcmp"  or
  156.         the  string  compare function.   It will return a 1  if  the
  157.         first string is larger than the second, zero if they are the
  158.         same  length  and have the same characters,  and -1  if  the
  159.         first  string  is  smaller  than the  second.   One  of  the
  160.         strings,  depending  on the result of the compare is  copied
  161.         into   the   variable  "mixed",   and   the   largest   name
  162.         alphabetically  is  printed  out.   It  should  come  as  no
  163.         surprise   to   you   that  "Zeke"  wins   because   it   is
  164.         alphabetically  larger,  length  doesn't  matter,  only  the
  165.         alphabet.  It might be wise to mention that the result would
  166.         also depend on whether the letters were upper or lower case.
  167.         There are functions available with your C compiler to change
  168.         the  case of a string to all upper or all lower case if  you
  169.         desire.   These  will be used in an example program later in
  170.         this tutorial.
  171.  
  172.                              COMBINING STRINGS
  173.  
  174.              The last four statements have another new feature,  the
  175.         "strcat",  or string concatenation function.   This function
  176.         simply  adds the characters from one string onto the end  of
  177.         another string taking care to adjust the NULL so  everything
  178.         is  still all right.   In this case,  "name1" is copied into
  179.         "mixed",  then two blanks are concatenated to  "mixed",  and
  180.         finally  "name2"  is concatenated to the  combination.   The
  181.         result  is printed out with both names in the  one  variable
  182.         "mixed".
  183.  
  184.              Strings  are  not difficult and are  extremely  useful.
  185.         You should spend some time getting familiar with them before
  186.         proceeding on to the next topic.
  187.  
  188.  
  189.  
  190.                                   Page 48
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.                       Chapter 7 - Strings and Arrays
  201.  
  202.  
  203.              Compile  and run this program and observe  the  results
  204.         for compliance with this definition.
  205.  
  206.                             AN ARRAY OF INTEGERS
  207.  
  208.              Load the file INTARRAY.C and display it on your monitor
  209.         for  an  example of an array of integers.   Notice that  the
  210.         array is defined in much the same way we defined an array of
  211.         char  in  order to do the string manipulations in  the  last
  212.         section.   We  have  12 integer variables to work  with  not
  213.         counting the one named "index".   The names of the variables
  214.         are "values[0]",  "values[1]", ... , and "values[11]".  Next
  215.         we have a loop to assign nonsense, but well defined, data to
  216.         each  of  the  12 variables, then print all  12  out.   Note
  217.         carefully that each element of the array is simply an  "int"
  218.         type  variable  capable  of storing an  integer.   The  only
  219.         difference  between the variables "index"  and  "values[2]",
  220.         for  example,  is  in the way that you  address  them.   You
  221.         should  have no trouble following this program, but be  sure
  222.         you  understand  it.  Compile and run it to see if  it  does
  223.         what you expect it to do.
  224.  
  225.                       AN ARRAY OF FLOATING POINT DATA
  226.  
  227.              Load  and display the program named BIGARRAY.C  for  an
  228.         example  of  a program with an array of "float"  type  data.
  229.         This  program has an extra feature to illustrate how strings
  230.         can  be  initialized.    The  first  line  of  the   program
  231.         illustrates to you how to initialize a string of characters.
  232.         Notice  that the square brackets are empty leaving it up  to
  233.         the  compiler  to count the characters and  allocate  enough
  234.         space  for  our  string  including  the  terminating   NULL.
  235.         Another string is initialized in the body of the program but
  236.         it  must be declared "static" here.  This prevents  it  from
  237.         being allocated as an "automatic" variable and allows it  to
  238.         retain  the  string once the program is started.   There  is
  239.         nothing  else new here, the variables are assigned  nonsense
  240.         data  and  the results of all the nonsense are  printed  out
  241.         along  with a header.  This program should also be easy  for
  242.         you to follow, so study it until you are sure of what it  is
  243.         doing before going on to the next topic.
  244.  
  245.                      GETTING DATA BACK FROM A FUNCTION
  246.  
  247.              Back  in chapter 5 when we studied functions,  I hinted
  248.         to you that there was a way to get data back from a function
  249.         by  using  an array,  and that is true.   Load  the  program
  250.         PASSBACK.C for an example of doing that.   In this  program,
  251.         we  define  an array of 20 variables  named  "matrix",  then
  252.         assign  some nonsense data to the variables,  and print  out
  253.  
  254.  
  255.  
  256.                                   Page 49
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.                       Chapter 7 - Strings and Arrays
  267.  
  268.  
  269.         the  first five.   Then we call the function "dosome" taking
  270.         along  the entire array by putting the name of the array  in
  271.         the parentheses.
  272.  
  273.              The  function  "dosome" has a name in  its  parentheses
  274.         also but it prefers to call the array "list".   The function
  275.         needs  to be told that it is really getting an array  passed
  276.         to  it  and that the array is of type "int".  Line  20  does
  277.         that  by  defining "list" as an integer  type  variable  and
  278.         including  the square brackets to indicate an array.  It  is
  279.         not necessary to tell the function how many elements are  in
  280.         the  array,  but you could if you so desired.   Generally  a
  281.         function  works with an array until some end-of-data  marker
  282.         is  found,  such  as  a NULL for a  string,  or  some  other
  283.         previously  defined  data or pattern.  Many  times,  another
  284.         piece of data is passed to the function with a count of  how
  285.         many elements to work with.  In our present illustration, we
  286.         will use a fixed number of elements to keep it simple.
  287.  
  288.              So far nothing is different from the previous functions
  289.         we  have called except that we have passed more data  points
  290.         to  the function this time than we ever have before,  having
  291.         passed 20 integer values.  We print out the first 5 again to
  292.         see if they did indeed get passed here.   Then we add ten to
  293.         each of the elements and print out the new values.   Finally
  294.         we return to the main program and print out the same 5  data
  295.         points.   We  find that we have indeed modified the data  in
  296.         the function,  and when we returned to the main program,  we
  297.         brought  the changes back.   Compile and run this program to
  298.         verify this conclusion.
  299.  
  300.                          ARRAYS PASS DATA BOTH WAYS
  301.  
  302.              We  stated during our study of functions that  when  we
  303.         passed data to a function,  the system made a copy to use in
  304.         the  function which was thrown away when we returned.   This
  305.         is not the case with arrays.   The actual array is passed to
  306.         the  function  and  the function can modify it  any  way  it
  307.         wishes  to.    The  result  of  the  modifications  will  be
  308.         available  back  in  the calling  program.   This  may  seem
  309.         strange  to  you that arrays are  handled  differently  from
  310.         single point data, but they are.  It really does make sense,
  311.         but  you  will  have  to wait until we get  to  pointers  to
  312.         understand it.
  313.  
  314.                          A HINT AT A FUTURE LESSON
  315.  
  316.              Another way of getting data back from a function to the
  317.         calling program is by using pointers which we will cover  in
  318.         the  next chapter.   When we get there we will find that  an
  319.  
  320.  
  321.  
  322.                                   Page 50
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.  
  332.                       Chapter 7 - Strings and Arrays
  333.  
  334.  
  335.         array  is in reality a pointer to a list of  values.   Don't
  336.         let  that  worry  you now,  it will make sense when  we  get
  337.         there.  In the meantime concentrate on arrays and understand
  338.         the  basics  of  them because when we get to  the  study  of
  339.         structures  we will be able to define some pretty  elaborate
  340.         arrays.
  341.  
  342.                         MULTIPLY DIMENSIONED ARRAYS
  343.  
  344.              Load  and  display  the file named  MULTIARY.C  for  an
  345.         example  of a program with doubly dimensioned  arrays.   The
  346.         variable "big" is an 8 by 8 array that contains 8 times 8 or
  347.         64  elements total.  The first element is  "big[0][0]",  and
  348.         the  last  is "big[7][7]".  Another array named  "large"  is
  349.         also  defined  which is not square to  illustrate  that  the
  350.         array need not be square.  Both are filled up with data, one
  351.         representing  a  multiplication table and  the  other  being
  352.         formed into an addition table.
  353.  
  354.              To illustrate that individual elements can be  modified
  355.         at will,  one of the elements of "big" is assigned the value
  356.         from  one of the elements of "large" after being  multiplied
  357.         by 22.  Next "big[2][2]" is assigned the arbitrary value  of
  358.         5,  and  this value is used for the subscripts of  the  next
  359.         assignment statement.  The third assignment statement is  in
  360.         reality  "big[5][5]  = 177" because each of  the  subscripts
  361.         contain  the value 5.  This is only done to illustrate  that
  362.         any  valid expression can be used for a subscript.  It  must
  363.         only meet two conditions, it must be an integer (although  a
  364.         "char"  will work just as well), and it must be  within  the
  365.         range of the subscript it is being used for.
  366.  
  367.              The  entire matrix variable "big" is printed out  in  a
  368.         square  form so you can check the values to see if they  did
  369.         get set the way you expected them to.
  370.  
  371.              You  should spend enough time to completely  understand
  372.         arrays since they are used in many C programs in one form or
  373.         another.  You will find many opportunities to use arrays, so
  374.         do not slight the material in this chapter.
  375.  
  376.  
  377.  
  378.  
  379.  
  380.  
  381.  
  382.  
  383.  
  384.  
  385.  
  386.  
  387.  
  388.                                   Page 51
  389.  
  390.  
  391.  
  392.  
  393.  
  394.  
  395.  
  396.  
  397.  
  398.                       Chapter 7 - Strings and Arrays
  399.  
  400.  
  401.         PROGRAMMING EXERCISES
  402.  
  403.         1.   Write  a  program with  three short  strings,  about  6
  404.              characters each, and use "strcpy" to copy "one", "two",
  405.              and  "three" into them.  Concatenate the three  strings
  406.              into one string and print the result out 10 times.
  407.  
  408.         2.   Define  two  integer  arrays,  each 10  elements  long,
  409.              called "array1" and "array2".  Using a loop,  put some
  410.              kind  of  nonsense data in each and add them  term  for
  411.              term  into  another 10 element  array  named  "arrays".
  412.              Finally,  print  all  results in a table with an  index
  413.              number.
  414.              1     2 +  10 =  12
  415.              2     4 +  20 =  24
  416.              3     6 +  30 =  36   etc.
  417.  
  418.              Hint; The print statement will be similar to;
  419.                 printf("%4d %4d + %4d = %4d\n",index,array1[index],
  420.                         array2[index],arrays[index]);
  421.  
  422.  
  423.  
  424.  
  425.  
  426.  
  427.  
  428.  
  429.  
  430.  
  431.  
  432.  
  433.  
  434.  
  435.  
  436.  
  437.  
  438.  
  439.  
  440.  
  441.  
  442.  
  443.  
  444.  
  445.  
  446.  
  447.  
  448.  
  449.  
  450.  
  451.  
  452.  
  453.  
  454.                                   Page 52
  455.