home *** CD-ROM | disk | FTP | other *** search
/ Software of the Month Club 1995 December / SOFM_Dec1995.bin / pc / dos / gi / ctutor / chap7.txt < prev    next >
Text File  |  1995-10-31  |  18KB  |  388 lines

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