You are on page 1of 13

CSC404 Programming II Tutorial & Practical Topic 1-3

FACULTY OF COMPUTER AND MATHEMATICAL SCIENCES


UNIVERSITI TEKNOLOGI MARA

TUTORIAL | LAB PRACTICAL


POINTER - FUNCTION
CSC404 –PROGRAMMING II
T5CS2302B1, T5CS2302B2, T5CS2692B

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

1.1 To understand pointers and pointers operators.


1.2 To be able to define and initialize pointers.
1.3 To understand the relationships among pointers, arrays and strings.
1.4 To be able to use pointers to pass arguments to function by reference.
2. INTRODUCTION:
Pointer is the address of an object (i.e. a specific memory location). It can refer to
different objects at different times. Pointers contain memory addresses as their values.
Pointers are used in C programs for a variety of purposes:

 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.

2.1 Pointer Declaration and Initialization


int *numPtr;
int *numPtr1, *numPtr2;
Initialize pointers to 0, NULL, or an address
int *numPtr = NULL;
int *numPtr = 0;
int *numPtr = #
2.2 Pointer Operators
Symbol & is called the address operator.
 It returns the address of the operand
For example:
int num = 7;
int *numPtr;
numPtr = # //numPtr gets address of num
//numPtr “points to” num

Symbol * is called indirection/dereferencing operator.


 It returns a synonym/alias of what its operand points to.
 *numPtr returns num (because numPtr points to num)
 * can also be used for assignment.
*numPtr = 10; //changes num to 10

Page 2|1
CSC404 Programming II Tutorial & Practical Topic 1-3

 Dereferenced pointer (operand of *) must be an lvalue (left values, can be


used on the left side of an assignment operator, no constants)

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

2. Declare the following pointer variables:


i. A pointer variable pfVar1 pointing to a float float *pfVar1;
ii. A pointer variable piNum pointing to an int int* piNum;
iii. A pointer variable pcChar1 pointing to a char char* pcChar1;

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;

4. Determine output for each of these program segments:


i.

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

5. Trace the following program and answer the questions.


#include <stdio.h>
int main (void)
{ //variables and pointers declarations
int a;
int b;
int c;
int *p;
int *q;
int *r;
a = 6;
b = 2;
Page 4|1
CSC404 Programming II Tutorial & Practical Topic 1-3

p = &b; //---------------> line 15 a=6


q = p; //----------------> line 17 b=2
addr C r = &c; //----------------> line 18
addr A p = &a; //----------------> line 20
8 *q = 8; //----------------> line 21
6 *r = *p; //----------------> line 23
*r = a + *q + *&c; //----------------> line 25
20 6+8+6
printf("\n a = %d\t b = %d\t c = %d\n", a, b, c); //---------------> line 27
printf("*p = %d\t*q = %d\t*r = %d\n", *p, *q, *r); //--------------- > line 28
printf("&a = %p\t&b = %p\t&c = %p\n", &a, &b, &c); //--------------- > line 29
printf(" p = %p\t q = %p\t r = %p\n", p, q, r); //---------------> line 30
return 0;
}
i. What p is pointing to in line 15? Address b
ii. Which variables are pointed by q and r in lines 17 and 18? q = variable p, value = 2
Give the value of the variable pointed by q. r = variable c

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

void funct1(int u, int v)


{
u = 2;
v = 6;
printf("\nWithin funct1:\t u = %d v = %d", u, v);
}
void funct2(int *pu, int *pv)
{
*pu = 2; Before calling funct1: u = 0, v = 0
Within funct1: u = 2, v = 6
*pv = 6;
After calling funct1: u = 0, v = 0
printf("\nWithin funct2:\t u = %d v = %d", *pu, *pv); Before calling funct2: u = 0, v = 0
} Within funct2 u = 2, v = 6
After calling funct2 : u = 2, v = 6
i. Write the output of the program.
ii. Comment on the difference in the values of u and v after calling function funct1 and
funct2. Value u and v after calling function 1 doesn't carry over to int main while
var\lue u and v after calling function 2 carry over to int main because pointer
save value in memory .
7. Given the following programs, trace the different variables and pointers as they are changed.
(Note : %p is used to display data type pointer (address))

Write the output of the program.

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

a=7 *aPtr = 7 &a = 0xcf6688 aPtr = 0xcf6688

Dereferencing pointer, *aPtr = 15 and a = 15


showing that * and & are complements of each other
&*aPtr = 0xcf6688
*&aPtr = 0xcf6688

8. The following program demonstrates the relationship between pointers and arrays.

Write the output of the program.

Page 7|1
CSC404 Programming II Tutorial & Practical Topic 1-3

Pointers operation on array


arrayPtr points to array[0] = 5
arrayPtr points to array[1] = 11
arrayPtr points to array[2] = 23
arrayPtr points to array[3] = 0
arrayPtr points to array[4] = 0

Regular operation on array:


array[0] = 5
array[1] = 11
array[2] = 23
array[3] = 0
array[4] = 0

9. What is the output for the following program segment?

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:

<return_type> <function_name> (arg_type arg_name, ...);

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);

2.2 Function Definition

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:

<return_type> <function_name> (arg_type arg_name, ...)


{
… statements …
}

For example:

int sum (int num1,int num2)


{
int add;
add = num1 + num2;
return(add);
}

Page 9|1
CSC404 Programming II Tutorial & Practical Topic 1-3

2.3 Function Call

Function call can be made in the main function or in other functions. Function call has the
following form:

(exp, exp ...)  exp is an expression – can be variable or constant

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;
}

ii. int fnFunc(int iA) Function only contain 1 parameter


{
return 2*iA
}
fnFunc(3);

int fnFunc1(int iA); Datatype of function must match


iii. int fnFunc1(int iA) with function declaration
{
int iB; P a g e 10 | 1
iB = 2*iA;
return iB;
}
CSC404 Programming II Tutorial & Practical Topic 1-3

iv. double sum(double iA,double iB)


{
double a,b;
return a+b;
}

void fnFunc(double iA,double iB)


{
double a,b,sumValue;
sumValue = sum(a,b);
}
Variable arent declared in both function
v.
int fnFunc3(int iN)
{
return iN*iN;
}

int iN;
iN = (int)fnFunc3(iN);
iN = Func3(iN);
iN = fnFunc3(8);
function fnFunc3 must be assigned to a variable

b) What is displayed by the program that follows?

35 35.80

P a g e 11 | 1
CSC404 Programming II Tutorial & Practical Topic 1-3

c) What is displayed by the program defined below

13.800

P a g e 12 | 1
CSC404 Programming II Tutorial & Practical Topic 1-3

d) Give the output of the following code:

iA = 3
iB = 3
iC = 4.000000

P a g e 13 | 1

You might also like