April 1999
CA208: 'C' PROGRAMMING

QUESTION 3

Total Marks: 20 Marks

Click here to access other questions

GRADE A
Sample student's solutions are indicated in green.
Return to Question 3

 

(a) (i) Differentiate between the terms call by reference and call by value. [2]
(ii) Describe the relationship between pointers and arrays. [2]
(i) In call by reference, the formal parameter using this scheme in a function/procedure will retain any changes to its value throughout the parameter lifetime in the program, even if it has exited the function/procedure.
In constrast, by using call by value, the value of the parameter( if there are any changes) will only be retained in the lifetime of the function or procedure.

(ii) An array can be declared as a pointer to the first element of the array. However, a pointer may not be an array.

 

(b) Write a procedure which takes two arguments - sum and product - such that the value of sum on exit is equal to the sum of the two values on entry, and the value of product on exit is equal to the product of the two values on entry. [4]
void operation (int *sum, int *product)
{    int *temp;
     temp=sum;
     *sum +=*product;
     *product = *product * *temp;
     return;
}

 

(c) Describe the effect on the following function on the argument dst, if x and y are null terminated strings, and dst is a sufficiently large character array.

void strfun(char *dst, char *x, char *y) {
   char *ptr;
   for(ptr = x; *ptr; *dst ++= *ptr++) {}
   for(ptr = y; *ptr; *dst ++= *ptr++) {}
}

[2]
This function copies string x and string y into string dst, such that dst will contain string x followed by string y on exit.

 

(d) Trace the following code segment and write down the output produced:

void main(void) {
   int digits[5] = {54, 53, 5, 13, 17};
   int *one, *two, i;
   one = &digits[0];
   two = digits;
   *one = 72;
   *two = 68;
   printf("first = %d\tsecond = %d\n", *one,
           digits[0]);
   one = one+2;
   two = two+1;
   printf("third = %d\tfourth = %d\n", *two, *one);
   ++one;
   digits[1] = 26;
   printf("fifth = %d\n", *one);
   for(i=0; i<5; i++)
       printf("\t%d", digits[i]);
}

[10]
first   =   68     second = 68
third =  53     fourth   = 5
fifth  =  13
            68    26    5   13   17