Announcements by ewghwehws



• P5 due Thursday
• Final Exam: Tuesday August 10, 8AM -
  10AM, Olin 155
• Want references? Remember to drop off a
  picture and note.
• Check your grades on the door of Upson

CS 100             Lecture 22               1
              Today’s Topics
•   More C
•   Data structures
•   Type definitions
•   Arrays
•   Struct and array parameters
•   Strings in C

CS 100                Lecture 22   2
           Recall Pointer Parameters
// Swap contents of variables pointed to by x and y
   void swap (int * x, int * y)
       {int tmp; tmp= *x; *x= *y; *y= tmp;}

Example of a call on procedure swap:

  int j= 17;
  int k= 24;
  swap(&j, &k);
  CS 100                  Lecture 22                  3
            Type Definitions and structs
• C provides a way to group a bunch of variables together,
  in what is called a struct.
• A typedef is similar to a class in Java, but the typedef
  doesn’t have methods

  C                                Corresponding Java
  typedef struct {                 public class Pair {
      int a;                             int a;
      char ch;                           char ch;
  } Pair;                          }

   CS 100                 Lecture 22                     4
           Use of types/structs
• Pair abc, xyz; ‘q’;
  abc.a = 5;

• Declaration of a struct variable in C actually
  creates the structure. No separate allocation is
  needed, as it is in Java (using new …).
• struct variables may be local to a function,just like
  ints, chars, and doubles.

CS 100                  Lecture 22                    5
                Struct parameters
Structs in C behave exactly like ints and chars in Java.
In declaring a variable of some struct type, no reference is
A struct variable is passed by value.
typedef struct { … } Pair;        void main (void) {
void fv (Pair p)                        Pair pr;
   {p.a= 42;}                           pr.a= 17;

   CS 100                   Lecture 22                   6
            Struct Pointer Parameters
If you want to change a component of a struct variable,
   pass a pointer to the struct
typedef struct { … } Pair; // As before
void fr (Pair * p)
   {(*p).a= 42;}
void main (void) {
   Pair pr;               Abbreviation. In C, instead of writing
   pr.a= 17;                            ( * p) . a
                          one can write ‘?’;
                                        p ->a

   CS 100                     Lecture 22                      7
malloc -- Dynamic Storage Allocation
• The standard library function malloc is used to obtain a
  block of storage.
• It is an extremely low-level function ---its argument is
  the number of bytes or words to be allocated (typically
  obtained using operator sizeof).
• It returns an untyped pointer, which must be cast to the
  desired type.
• Allocated storage must be explicitly freed by calling
  procedure free. There is no automatic garbage

  CS 100                  Lecture 22                   8
          Example with malloc
   #include <stlib.h>

   Pair * p;   /* p is a pointer to a Pair*/

   p= (Pair *) malloc( sizeof(Pair) );
   p->a= 17;


CS 100                    Lecture 22           9
                      C Arrays
• C arrays are syntactically similar to arrays in Java, but
  the semantics (meaning) is different.
• Declaring an array in C actually allocates it.
• Declaring an array of structs allocates a block of storage
  with space for all the structs in the array.
• The array name is effectively a pointer to the first
  element of the array.
• Array indices start at 0.
• There is no built-in function to determine the length of
  an array.

   CS 100                  Lecture 22                  10
                 Array examples
     int b[10];
     Pair v[10];
     int k;
     for (k= 0; k < 10; k= k+1)
            b[k]= 0;
     v[0].a= 10;
     v[2].ch= ‘!’;

1[b] = 33; x = 5[b];
   CS 100                  Lecture 22   11
          Arrays as parameters
• When an array name is used as a parameter, the
  “value” of the parameter is a pointer to its first
• Therefore, array parameters are always pointers.
• The called function refers to the argument array
  directly. No copy of the array is made.
• An & is usually not needed or appropriate for an
  array argument

CS 100                  Lecture 22                     12
         Example of array parameter
/* Set s[0..n-1] to 0.0 */
void zero (double a[ ] , int n) {
   int k;
   for (k= 0; k<n; k= k+1)
   a[k]= 0.0;
void main(void) {
       double b[100];
       zero(b, 100);
CS 100                   Lecture 22   13
            Arrays and Pointers
• Because array names are treated as pointers, C
  programmers tend to use pointers and arrays
  somewhat interchangeably.
• Procedure zero could have been written with a
  pointer parameter, as shown on the next slide.
• It could still be called with an array name as

  CS 100               Lecture 22              14
         Array parameter as pointer
/* Set s[0..n-1] to 0.0 */
       void zero (double * a , int n) {
              int k;
              for (k= 0; k<n; k= k+1)
                           a[k]= 0.0;

         void main(void) {
               double b[100];
               zero(b, 100);
CS 100                   Lecture 22       15
       Arrays of structs as parameters
An array of structs is treated like any other array. If a parameter is an
  array of structs, a pointer to the begin-ning of an array ( array
  name) is the appropriate argument.
  /* Store 0’s and blanks in pa[0..n-1] */
  function zap (Pair pa [ ], int n) {
       int k;
       for (k= 0; k<n; k++)
               {pa[k].a= 0; pa[k].ch= ‘ ’;}

  void main( void ) {
        Pair ray[42];
        /* initialize ray[0..16] to 0’s and blanks */
                 zap(ray, 17);
  } CS 100                          Lecture 22                     16
Structs containing arrays as parameters
Structs may contain arrays --but remember that struct
  arguments will be copied unless pointers are used.
    typedef struct {
        int a[10];
        } Ray;
    void isCopied(Ray r) {
        r.a[2]= -1; /* Doesn’t change the argument */ }
    void notCopied(Ray * r) {
        r.a[2]= -1; /* Changes the argument */ }
    void main (void) {
        Ray a;
    CS 100                     Lecture 22                 17
                 Strings in C
• In C, a string is an array of characters.
• The array contains the characters in the string
  followed by an extra char (byte) containing a
  binary zero.
• Example: “hello” is represented by an array:

                h e    l l o         \0

CS 100                  Lecture 22                  18
                    More on Strings
• Because strings are arrays, and because array names are
  basically pointers, the assignment and comparison
  operations manipulate pointers, and not the strings

        char s[ ]= “hello”;
        char t[ ]= s; /* s and t point to same place*/
        s[1]= ‘?’;
        /* s and t are now “h?llo” */

• Standard library strlib.h contains routines to copy string
  values, catenate them, compare them, etc.
   CS 100                        Lecture 22              19
• Tomorrow, Friday: review,
  review, review
• Check your grades on the door of
  5141 Upson this week!
• Remember: Last chance to see
  me about your final grade is
  Wednesday before 2PM
• Final is Tuesday at 8AM, Olin
  155. Bright and early!
  CS 100              Lecture 22     20

To top