LAB 11 Pointers: MDJ11103 - Computer Programming Laboratory Module
LAB 11 Pointers: MDJ11103 - Computer Programming Laboratory Module
LAB 11
POINTERS
1
MDJ11103 - Computer Programming Laboratory Module
1. OBJECTIVES:
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.
int *numPtr;
int *numPtr1, *numPtr2;
For example:
int num = 7;
int *numPtr;
numPtr = # //numPtr gets address of num
//numPtr “points to” num
Dereferenced pointer (operand of *) must be an lvalue (left values, can be used on the left side
of an assignment operator, no constants)
2
MDJ11103 - Computer Programming Laboratory Module
3. TASKS:
int *ptr_int
int *ptr1_int;
int num1;
3.2 Given the following programs, trace the different variables and pointers as they are changed.
(Note : %p is used to display data type pointer (address))
a. #include <stdio.h>
int main ()
{
int a;
int *aPtr; //aPtr is a pointer to an integer
a = 7;
aPtr = &a; //aPtr sets to address of a
3
MDJ11103 - Computer Programming Laboratory Module
return 0;
}
b. #include <stdio.h>
a = 6;
b = 2;
p = &b; //---------------> line 15
q = p; //----------------> line 17
r = &c; //----------------> line 18
return 0;
}
4
MDJ11103 - Computer Programming Laboratory Module
ii. Which variables are pointed by q and r in lines 17 and 18? Give the value of the variable
pointed by q.
__________________________________________________________________________
iii. Referring to line 25, is the content of variable c changing? 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.
__________________________________________________________________________
__________________________________________________________________________
3.3 The following program demonstrates the relationship between pointers and arrays. Type,
compile and run the following program.
#include <stdio.h>
#define SIZE 5
int main()
{
int array[SIZE]={5,11,23};
int *arrPtr;
int i,j;
return 0;
}
5
MDJ11103 - Computer Programming Laboratory Module
Sample Output:
Enter Test 1 and Test 2 marks : 50 80
Enter Test 1 and Test 2 marks : 40 60
Enter Test 1 and Test 2 marks : 30 70
Enter Test 1 and Test 2 marks : 84 56
Enter Test 1 and Test 2 marks : 55 90
Test1[0] = 50 Test2[0] = 80
Test1[1] = 40 Test2[1] = 60
Test1[2] = 30 Test2[2] = 70
Test1[3] = 84 Test2[3] = 56
Test1[4] = 55 Test2[4] = 90
int main()
{
int test1[5]; int *test1Ptr;
int test2[5]; int *test2Ptr;
int total[5]; int *totalPtr;
int i, j;
test1Ptr = test1;
test2Ptr = test2;
totalPtr = total;
6
MDJ11103 - Computer Programming Laboratory Module
3.4 The following program demonstrates the relationship between pointers and functions (pass by
reference). Type, compile and run the following program.
#include <stdio.h>
7
MDJ11103 - Computer Programming Laboratory Module
b. Comment on the difference in the values of u and v after calling function funct1 and funct2.
c. Write a program to read two integers and perform the division operation. The first number
entered by the user should be divided by the second number. Print out the quotient and the
remainder from that division operation. This program will demonstrate the use of pointers
to pass arguments to function by reference.
Sample Output:
8
MDJ11103 - Computer Programming Laboratory Module
void fillArray (int *, int); - to input marks into a one dimensional (1-D) array
void swapArray(int *, int); - to swap the array elements
void printArray (int *, int); - to print the array elements
Sample Output:
Enter 5 marks : 45 66 12 34 55