Lecture 4: Complex Datatypes
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Function Prototypes
*** You may have run into a CodeWarrior warning if you have
tried to program with functions:
"warning function has no prototype"
*** function prototype gives type information about a function:
int power(int base, int exponent);
int power(int,int);
*** names are unnecessary
*** a function declaration: you promise to write the function
definition later.
*** used for mutually recursive functions and most often
for multi-file programs
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Preprocessor(K & R, Chapt. 4.11):
*** Before compilation, the C preprocessor makes a pass over the program
*** # at the beginning of a line indicates a preprocessor command
*** #include == > include some other file
*** include your own definitions or library definitions
*** eg: #include < stdio.h > == > include the standard I/O library
== > preprocessor knows where to look for these
programs and includes the declarations
for them
== > functions printf and scanf are here
*** < library.h > == > a .h files is a header file with declarations
*** #define name definition == > defines a macro
*** the preprocessor replaces the macro with its definition
wherever it is used
*** C convention is to use All-caps for the macro name
*** Eliminates "magic numbers" from your program
*** eg:
#define TABLESIZE 100
int main(void)
{
int i;
for (i=0; i < TABLESIZE; i++)
/* Print Table Row */
...
}
*** the preprocessor macro expansion is very powerful (obfuscated C -
adventure game) but you should only use it to define constants
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Arrays:
*** arrays store a series of values of the same type
*** eg:
#define MAXSIZE 20
int vec[MAXSIZE];
float fvec[3]
*** arrays are indexed from 0 to size-1
vec: 0..19
fvec: 0..2
*** the ith entry of an array vec == vec[i]
*** eg:
#define SIZE 20
int main(void)
{
int i;
int v1[SIZE], v2[SIZE], res[SIZE];
int next;
/* Scan in v1 */
for(i = 0; i < SIZE; i++){
scanf(``%d'',& next);
v1[i] = next;
}
/* Scan in v2 */
for(i = 0; i < SIZE; i++){
scanf(``%d'',& next);
v2[i] = next;
}
/* Multiply Vectors */
for(i=0; i < SIZE; i++){
res[i] = v1[i] * v2[i];
}
/* Print result */
for(i = 0; i < SIZE; i++){
printf(``%d '',res[i]);
}
return 0;
}
Initializing Arrays:
int a[5] = {0,1,2,3,4};
int b[] = {0,1,2,3,4};
Multi-dimensional Arrays
#define ROWS 10
#define COLS 10
/* define matrix A */
int A[ROWS][COLS];
int x[COLS], b[ROWS];
int i,j;
...
/* matrix vector multiply */
for(i=0; i < ROWS; i++)
for(j=0; j < COLS; j++){
b[i] += A[i][j] * x[j];
}
for(i=0; i < ROWS; i++)
printf("%d ",b[i]);
Initializing Multi-dimensional Arrays
int A[2][3] =
{{1,2,3},
{4,5,6}};
*** the rightmost index forms the innermost "grouping"
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Structures
*** arrays contain a series of elements of the same type
*** structures contain elements of different types
*** struct == record in some languages
struct date {
int month;
int day;
int year;
} d1, d2, d3;
struct date d4;
struct date d5;
*** "struct date" is used as a type name
struct date d4, d5;
== > analogous to
int d4, d5;
*** date is called the structure's tag
*** day, month, year are labels.
scanf("%d / %d / %d", & d.month, & d.day, & d.year);
printf("%d / %d / %d\n", & d.month. & d.day, & d.year);
Structure Initialization
*** like arrays:
struct date d = {
10,
6,
1997
};
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Structures containing structures:
struct period {
struct date start;
struct date end;
};
struct period {
struct {int month; int day; int year} start, end;
};
Initialization:
struct period holiday = {{5,2,1997}, {8,26,1997}};
int start_day = holiday.start.day;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Arrays and Structures:
struct employee {
int ssn; /* social security number */
int hours; /* hours per week */
float pay; /* amount they are paid */
};
#define EMPLOYEE_MAX 3
int main(void) {
struct employee payroll[EMPLOYEE_MAX] =
{{444523234, 40, 200.00},
{513123124, 50, 250.00},
{523554635, 40, 220.00}}
float av_pay = 0.0, av_hours = 0.0;
int i;
for (i=0; i < EMPLOYEE_MAX; i++){
av_pay += payroll[i].pay;
av_hours += payroll[i].hours;
}
printf("average pay = %f; average hours = %f\n",
av_pay/EMPLOYEE_MAX,
av_hours/EMPLOYEE_MAX);
return 0;
}