INFORMATION & COMPUTER SCIENCE DEPARTMENT, KFUPM

ICS – 201 SECTION 55 & 56 (992 Semester)

INTRODUCTION TO COMPUTER SCIENCE

LAB  #06 TOPIC: User-Defined Functions and Pointers

 

Objective:       Understand how to declare and use pointer variables

                        Apply pointers to functions with output parameters.

 


What is a pointer variable and how is it declared?

·        A pointer is a variable that can hold the memory address (lvalue) of another variable (or the constant NULL ).

·        It is a legitimate data type in C whose range is the address space of the computers’ memory and it can be printed using the %p format specifier. 

·        Pointer variables are usually associated with a specific type – int pointer, char pointer, etc. 

·        A pointer variable is declared by preceding it its name with an asterisks ’*’

E.g.      int *x

float *y, *z

char* ch

 

Assignment of values to pointer variables

·        A pointer variable can be assigned a value (address of another variable) at declaration.

E.g. int i, *p=&i, k=5, *q=&k;

·        A pointer variable can also be assigned a value using the assignment statement.

E.g.      q=&i

·        When the address of a variable (e.g. i) is assigned to a pointer variable (q = &i), the value of the variable can be accessed in two ways:

  1. Directly, using variable name:  e.g.  i =10
  2. Indirectly, using the dereference operator, *, e.g.   *q=20

 

Pointer assignment & equality

·        Pointers of the same type can be assigned and compared. Consider the declarations:

          int a=20, *p1=&a, b=20, *p2=&b;

·        At this point,  *p1= =*p2 is true,  but  p1 = = p2 is false.

·        However, after the assignment, p1=p2;  both expressions are true.

·        Note that after the assignment, the variable a can only be accessed directly; because the pointer p1 no longer points to it, but the variable b can now be accessed directly as b, indirectly as *p1 or indirectly as *p2.

 

Functions that return one result

·        Many functions receive one or more arguments and return only one result.

e.g. long fact(int n);

·        These types of functions returns their result through the function name using the return statement.  For example, the following implements the factorial function:

long fact(int n)

{    long ans=1;

     int i;

     for (i=n; i>1, i--)

          ans*=i;

     return ans;

}

·        The parameters in these types of functions are used to receive actual values (rvalues) from the main program (or calling functions)  they are therefore called input parameters.

·        These functions can only be called with variables that already have values assigned to them.  For example, the following main program calls the function fact above.

#include <stdio.h>

main()

{    int num;

     long factorial;

     printf(”Type your number >”);

     scanf(”%d”,num);

     factorial=fact(num);

     printf(”The factorial of %d is %ld\n ”,num,factorial);

     return 0;

}

·        Notice that when the function is called:

factorial = fact(num);

The value (rvalue) of the input argument num is copied to the input parameter n.  This is called call-by-value.

 

Functions that returns more than one result

·        Some functions are required to return more than one result.  Example;  a function that converts time in seconds into hours, minutes and seconds.

·        Since the return statement can only return one result, it cannot be used in this case.

·        Our best option is to use output parameters.

·        In C, output parameters are declared as pointer variables as shown by the following example:

void get_time (int n, int *phr, int *pmin, int *psec)

{    *phr = n/3600;

     *pmin = (*phr % 3600)/60;

     *psec = n – *phr * 3600 - *pmin * 60;

}

·        In the above, n is an input parameter used to receive the time in seconds from the calling function;  phr, pmin, and psec are output parameters – pointer variables used to receive the address of the variables where output should be sent.

·        Notice the use of the derefencing operator ‘*’ to indirectly access the actual variables that are being assigned the output

    as in:                   *phr = n/3600

·        The following main program shows how the function get_time can be called.

#include <stdio.h>

{    int num, hr, min, sec;

     printf(”Enter your time in seconds >”);

     scanf(”%num”);

     get_time(num, &hr, &min, &sec);

     printf(”%d seconds = %d hours %d minutes %d

              seconds\n”, num, hr, min, sec);

     return 0;

}

·        Notice that the function call:

Get_time(num, &hr, &min, &sec);

Passes the value (rvalue) of num to the function, but passes only the addresses (lvalues) of hr, min, and sec using the address of operator ‘&’.  The latter is called call-by-reference. 

 

Exceptions: 

·        As exceptions to the above rules on input and output parameters are arrays (and strings). 

·        An array can only be passed as an output parameter (by-reference) even if the function is not going to change its values. 

·        However, with arrays, the ‘*’ is not required when specifying the function prototype.  We just use the array name followed by []. Also, the ‘&’ operator is not required when calling the function.  We just use the array name.

 

Programming Exercises

1.         Write a function that takes 3 integers and returns the maximum, the minimum, and the average. Write a main program to test the function.

 

2.         Write function that takes the coefficient {a, b, c} of a quadratic equation and returns the roots (x1, x2) of the equation. You can use the formula, .  Your function should return status 1 if the equation has real roots and 0 otherwise.  Test your function by writing a main program that calls the function and print the roots if they exists, or an appropriate message otherwise. You may use the values {1, -5, 6}  and {1, 2, 3}.

 

3.         Write a function that takes 4 float values and return them after adding the minimum value to each of them. Write a main program to test the function. (Note: Your program should have only 4 parameters).

 

Home Work

 

1.                  Answer question 22 of page 313 of your book.

2.                  Write a function that receives a floating point argument representing an amount of money in SR, and returns through output parameters, the number of each of the following coins that can be obtained from the original amount. The coins are:

50 halala, 25 halala, 10 halala and 5 halala.  Write a main program to test the function.