Professional Documents
Culture Documents
CSC404 TP3
CSC404 TP3
PART A (POINTER)
1. Objectives
2. Introduction
2.1 Pointer Declaration and Initialization
2.2 Pointer Operators
3. Exercises
PART B (FUNCTION)
1. Objectives
2. Introduction
2.1 Function Prototype
2.2 Function Definition
2.3 Function Call
3. Exercises
PART A (POINTER)
1. OBJECTIVES:
Page 1|1
CSC404 Programming II Tutorial & Practical Topic 1-3
To return more than one value from a function (using pass by reference).
To create and process strings.
To manipulate the contents of arrays and structures.
To construct data structures whose size can grow or shrink dynamically.
Page 2|1
CSC404 Programming II Tutorial & Practical Topic 1-3
EXERCISES
1. Specify whether the statements below are valid or invalid. Give the reason for invalid
statement.
i. int *ip; Valid
ii. int i; double *dp = &i; Invalid. int pointer cannot be converted to double pointer
3. For each of the following, write a single statement that performs the indicated task. Assumed
that variables iValue1 and iValue2 of type int have been declared, and that iValue1 has been
initialized to 100,000
i. Declare the variable piNum1 to be a pointer to an object of type int. int* piNum1;
ii. Assign the address of variable iValue1 to pointer variable piNum1. piNum1 = &iValue1;
iii. Print the value of the object pointed to by piNum1. cout<<*piNum1;
11 11
ii.
float val = 4.50;
float *vPtr = &val; Page 3|1
*vPtr *= 5 22.50
printf(”%.2f”, *vPtr)
CSC404 Programming II Tutorial & Practical Topic 1-3
iii.
20.50
iii. Referring to line 25, is the content of variable c changing? Yes, value of c is 20
If yes, what is the value of c. If no, state your reason
iv. Display the outputs in lines 27, 28, 29 and 30 on the screen.
a=6 b=8 c = 20 &a = 0000022fe34 &b = 0000022fe30 &c = 0000022fe2c
p=6 q=8 r = 20 p = 0000022fe34 q = 0000022fe30 r = 0000022fe2c
6. The following program demonstrates the relationship between pointers and functions (pass
by reference).
#include <stdio.h>
void funct1(int u, int v);
void funct2(int *pu, int *pv); //function prototype for funct2 which uses pointers
//(pass by reference)
int main()
{
int u = 0;
int v = 0;
printf("\nBefore calling funct1: u = %d v = %d", u, v);
funct1(u, v);
printf("\nAfter calling funct1: u = %d v = %d", u, v);
printf("\n\nBefore calling funct2: u = %d v = %d", u, v);
funct2(&u, &v);
printf("\nAfter calling funct2: u = %d v = %d\n", u, v);
return 0;
}
Page 5|1
CSC404 Programming II Tutorial & Practical Topic 1-3
Page 6|1
CSC404 Programming II Tutorial & Practical Topic 1-3
The value of a is 7
The value of *aPtr is 7
The address of a is
the value of aPtr is 0xcf6688
8. The following program demonstrates the relationship between pointers and arrays.
Page 7|1
CSC404 Programming II Tutorial & Practical Topic 1-3
4
3
3 9
27
81
81
PART B (FUNCTION)
1. OBJECTIVES:
1.1 To apply functions as building blocks of programs.
1.2 To write C programs using functions.
2 INTRODUCTION:
A C program is generally formed by a set of functions, which subsequently consist of many
programming statements. Using functions, a large computing task can be broken into smaller
Page 8|1
CSC404 Programming II Tutorial & Practical Topic 1-3
ones. Functions can be created to execute small, frequentlyused tasks. In C, there are predefined
functions or sometimes called standard functions, and user-defined functions.
Predefined functions are already available functions which can be used, called library, such as
stdio.h, math.h, string.h and stdlib.h. The library name must be included at the top of the source
code (preprocessor directive).
User-defined functions in a program are built using:
Function prototype
Function definition
Function call
2.1 Function Prototype
To use function in a program, it has to be declared at the beginning of a program, using function
prototype. Function prototype has the following form:
For example:
//function named sum with two (2) arguments and returns integer data type.
int sum (int num1,int num2);
//function named sum with two (2) arguments but does not return any data.
void sum (int num1,int num2);
Function definition is the function body. It is used to define what the function does.
The coding is written inside the function definition. Function definition has the following form:
For example:
Page 9|1
CSC404 Programming II Tutorial & Practical Topic 1-3
Function call can be made in the main function or in other functions. Function call has the
following form:
For example:
result = sum(x,y);
EXERCISES
10. What is the type of variables used as parameters in the function header?
int, double, float, string, char , bool
11. Write a function named sum() that takes two positive integer arguments, first and last. The
function returns the sum of all the integers between first and last inclusive.
For example: int sum(int a,int b)
{
cout<< sum(2,5) <<endl; // this will print 14 int result=0;
for(int i = a;i<=b;i++)
cout<< sum(7,7) <<endl; // this will print 7 {
result += a;
}
return result;
12. }
a) Identify and correct the errors, if there are any, in the following program segments.
Briefly explain why.
i. Data type doesnt match
int fnFunc(double iA);
int fnFunc(double iA)
{
return 10*iA;
}
int iN;
iN = (int)fnFunc3(iN);
iN = Func3(iN);
iN = fnFunc3(8);
function fnFunc3 must be assigned to a variable
35 35.80
P a g e 11 | 1
CSC404 Programming II Tutorial & Practical Topic 1-3
13.800
P a g e 12 | 1
CSC404 Programming II Tutorial & Practical Topic 1-3
iA = 3
iB = 3
iC = 4.000000
P a g e 13 | 1