CSPP 51038 - Tutorial 2

zip file of all *.c programs

Arrays and Strings

In our last tutorial, we talked about how strings are arrays of char's, terminated with the null character, '\0':
ex1_string.c   text

The C standard library provides a number of fuctions to examine and manipulate strings. Here a few common string functions:

Also see K+R, Appendix B3, for more useful string function.

Observe that with these functions, a result is written into a buffer that is specified by the user.
ex2_buffer.c   text

Lets look again at the scanf library function, which reads input from standard in. ex3_scanf.c   text

So, notice that the array variable:

char message[]

actually stores a memory addres of the first object of an array.
That is, char message[] is a variable that stores the memory address of the character at message[0].

Pointers!

Let's talk about another type of variable that stores a memory address: Pointers
( Reading: K+R 5.1-5.3,5.5 )

Pointer declaration

In a variable declaration, we use * to indicated that a variable is a "pointer-to-something"
(Beware, * has another use outside of variable declarations.)

char* mystring;  /* pointer to char == char array == a string! */
int* pi;       /* pointer to int */

Here are 3 equivalent ways to declare a pointer to an int.
In all cases, pi is of type pointer-to-int, not type int:

int* pi;
int * pi;
int *pi;

After declaration - dereferencing a pointer?

Remember, pointer holds an address of a variable or an array object.
We use the * to "deference" a pointer, ie, to access the value at that memory addresss.
Example: ex4_intptr.c   text
Be sure you understand every line of ex4_intptr.c.
When you are done, uncomment the last line and rerun the program. What happens, and why?

Basic pointer arithmetic

We can also increment, decrement, and compare pointers. A common application is to use pi++ to increment a pointer to the next element in an array: ex5_increment.c   text

Pointers as function arguments

In some cases we use pointers as function arguments when we want to modify the values of those arguments. This is known as call by reference.
Complete this example so that our swap2 function works!
ex6_swap.c   text

Operator precedince

Note that [] is applied before * and &, so

&arr[0] == &(arr[0])

See K+R p 53 for more on operator precedence.

Structures

Reading:K+R 6.1-6.4,6.7
Structures are used when we want to combine to or more data types into a single variable. For example, we may want a variable for a 2-D coordinate, that contains information for each dimention (from K+R p129):

/* structure definition */
struct point {   /* here, point is the "tag" */   float x;
  float y;
};

/*variable declarations */
struct point start;
struct point end;

Above we declare 2 variables of type struct point. This is equivalent to:

struct point {
  float x;
  float y;
} start, end;

We use . (dot) to access the members of a structure. For example, see main below:

struct point {
  float x;
  float y;
};

int main ()
{
  struct point start;
  struct point end;
  ...
  /* Print x and y co-ordinates from each both start and end variables */
  printf(" Point 1: ( %.2f, %.2f ) \n",start.x,start.y);
  printf(" Point 2: ( %.2f, %.2f ) \n",end.x, end.y);
  ...
}

typedef and structures

It is very common to use typedef with structure types: (See K+R section 6.7)

typedef struct point {  /* point tag is optional here */
  float x;
  float y;
} point;


int main ()
{
  point start;
  point end;

  ...
  /* Print x and y co-ordinates from each both start and end variables */
  printf(" Point 1: ( %.2f, %.2f ) \n",start.x,start.y);
  printf(" Point 2: ( %.2f, %.2f ) \n",end.x, end.y);
  ...
}

Pointers to structures

As with primitive types, we can declare a pointer to a structure:
Use the -> operator with structure pointers to access members.

struct point {
  float x;
  float y;
};

int main ()
{
  /* variable declarations: pointers to structures */
  struct ppoint * pstart;
  struct ppoint * pend;
  ...
  /* Print x and y co-ordinates from each both start and end variables */
  printf(" Point 1: ( %.2f, %.2f ) \n", pstart->x, pstart->y);
  printf(" Point 2: ( %.2f, %.2f ) \n", pend->x, pend->y);
  ...
}

Structure members

Structures can contain arrays, as well as other structures. Look at the path and rect structures below:

#define STRLEN 256

struct point {
  float x;
  float y;
};

struct rect {
  struct point SW;
  struct point NE;
};

struct triangle{
  struct point point_arr[3];     /* array of points */
  char name[STRLEN];     /* character array */
};

We can see how these are used in this example:   ex7_struct.c   text

Exercise



Written by Sonjia Waxmonsky, wax at cs uchicago edu