/* File: access.c
This program prints out the values of pointers and values of
dereferenced pointer variables.
*/
#include <stdio.h>
main()
{ int *iptr, /* integer pointer */
i1;
printf("Pointers: Direct and Indirect Access\n\n");
/* initializations */
i1 = 10;
iptr = &i1; /* iptr points to the object whose name is i1 */
/* print value of iptr, i.e., address of i1 */
printf("iptr = %u\n", iptr);
/* print value of the object accessed indirectly and directly */
printf("*iptr = %d, i1 = %d\n", *iptr, i1);
*iptr = *iptr * 10; /* value of *iptr changed */
/* print values of the object again */
printf("*iptr = %d, i1 = %d\n", *iptr, i1);
}
/* File: indincr.c
Program illustrates indirect access
to x by a function indirect_incr().
Function increments x by 1.
*/
#include <stdio.h>
void indirect_incr(int * p);
main()
{ int x;
printf("***Indirect Access***\n");
x = 7;
printf("Original value of x is %d\n", x);
indirect_incr(&x);
printf("The value of x is %d\n", x);
}
/* Function indirectly accesses object in calling function. */
void indirect_incr(int * p)
{
*p = *p + 1;
}
/* File: pay6.c
Other Files: payutil.c
Header Files; tfdef.h, payutil.h
The program gets payroll data, calculates pay, and prints out
the results for a number of people. Modular functions are used
to get data, calculate total pay, print data, and print results.
*/
#include <stdio.h>
#include "tfdef.h"
#include "payutil.h"
main()
{
/* declarations */
int id_number, moredata;
float hours_worked, rate_of_pay, regular_pay, overtime_pay, total_pay;
/* print title */
printf("***Pay Calculation***\n\n");
/* get data and initialize loop variable */
moredata = get_data(&id_number, &hours_worked,
&rate_of_pay);
/* process while moredata */
while (moredata) {
total_pay = calc_pay(hours_worked, rate_of_pay, ®ular_pay,
&overtime_pay);
print_data(id_number, hours_worked, rate_of_pay);
print_pay(regular_pay, overtime_pay, total_pay);
moredata = get_data(&id_number, &hours_worked,
&rate_of_pay);
}
}
/* File: payutil.c */
#include <stdio.h>
#include "tfdef.h"
#include "payutil.h"
/* Function prints out the input data */
void print_data(int id, float hours, float rate)
{
printf("\nID Number = %d\n", id);
printf("Hours Worked = %f, Rate of Pay = %f\n",
hours, rate);
}
/* Function prints pay data */
void print_pay(float regular, float overtime, float pay)
{
printf("Regular Pay = %f, Overtime Pay = %f\n",
regular, overtime);
printf("Total Pay = %f\n", pay);
}
/* Function calculates and returns total pay */
float calc_pay(float hours, float rate, float * preg, float * pover)
{ float total;
if (hours > REG_LIMIT) {
*preg = REG_LIMIT * rate;
*pover = OT_FACTOR * rate * (hours - REG_LIMIT);
}
else {
*preg = hours * rate;
*pover = 0;
}
total = *preg + *pover;
return total;
}
/* Function reads in the payroll data */
int get_data(int * pid, float * phrs, float * prate)
{
printf("Type ID Number, zero to quit: ");
scanf("%d", pid);
if (*pid <= 0) /* if ID number is <= 0, */
return FALSE; /* return 0 to calling function */
printf("Hours Worked: "); /* ID number is valid, get data */
scanf("%f", phrs);
printf("Hourly Rate: ");
scanf("%f", prate);
return TRUE; /* valid data entered, return 1 */
}
/* File: payutil.h */
#define REG_LIMIT 40
#define OT_FACTOR 1.5
int get_data(int *pid, float *phrs, float *prate);
void print_data(int id, float hrs, float rate);
void print_pay(float regular, float overtime, float total);
float calc_pay(float hours, float rate, float * pregular,
float * povertime);
/* File: sqcube.c
Program uses a function that returns a square of its argument and
indirectly stores the cube.
*/
#include <stdio.h>
double sqcube(double x, double * pcube);
main()
{ double x, square, cube;
printf("***Directly and Indirectly Returned Values***\n");
x = 3;
square = sqcube(x, &cube);
printf("x = %f, square = %f, cube = %f\n",
x, square, cube);
}
/* Function return square of x, and indirectly stores cube of x */
double sqcube(double x, double * pcube)
{
*pcube = x * x * x;
return (x * x);
}
/* File: swapfnc.c
Program uses a function to swap values of two objects.
*/
#include <stdio.h>
/* arguments of swap() are integer pointers */
void swap(int * p1, int * p2);
main()
{ int dat1 = 100, dat2 = 200;
printf("Original values: dat1 = %d, dat2 = %d\n", dat1, dat2);
swap(&dat1, &dat2);
printf("Swapped values: dat1 = %d, dat2 = %d\n", dat1, dat2);
}
/* Function swaps values of objects pointed to by ptr1 and ptr2 */
void swap(int * ptr1, int * ptr2)
{ int temp;
temp = *ptr1;
*ptr1 = *ptr2;
*ptr2 = temp;
}
/* File: tfdef.h */
#define TRUE 1
#define FALSE 0