You are on page 1of 9

MDJ11103 - Computer Programming Laboratory Module

LAB 11
POINTERS

Diploma Manufacturing Engineering


Faculty of Mechanical Engineering Technology
Universiti Malaysia Perlis

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.

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

 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:

3.1 a. Declare the following pointer variables:


i. A pointer variable pi_ptr pointing to a float
____________________________________

ii. A pointer variable num_ptr pointing to an int


____________________________________

iii. A pointer variable pc_ptr pointing to a char


____________________________________

b. Consider the declarations below:

int *ptr_int
int *ptr1_int;
int num1;

Answer either True or False for the following statements:

1. ptr_int = num1; - ptr_int gets the address of num1 Ans: ___________


2. ptr1_int = &num1; - ptr1_int points to num1 Ans: ___________
3. ptr_int = ptr1_int; - generates error during compilation Ans: ___________
4. *ptr1_int = 20; - assigns num1 with 20 Ans: ___________
5. printf(“%d”,*ptr1_int); - prints the value of 20 on the screen Ans: ___________

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

printf ("\nThe value of a is %d"


"\nThe value of *aPtr is %d",a,*aPtr);

printf ("\nThe address of a is %p"


"\nThe value of aPtr is %p",&a,aPtr);

3
MDJ11103 - Computer Programming Laboratory Module

printf("\na = %d\t*aPtr = %d\t\t&a = %p\taPtr = %p\n",a, *aPtr, &a, aPtr);


*aPtr = 15;

printf("\nDereferencing pointer, *aPtr = %d and a = %d\n", *aPtr,a);


printf ("\nShowing that * and & are complements of each other\n");
printf ("&*aPtr = %p\n",&*aPtr);
printf ("*&aPtr = %p\n",*&aPtr);

return 0;
}

Write the output of the program.

b. #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;
p = &b; //---------------> line 15

q = p; //----------------> line 17
r = &c; //----------------> line 18

p = &a; //----------------> line 20


*q = 8; //----------------> line 21

*r = *p; //----------------> line 23


*r = a + *q + *&c; //----------------> line 25

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

4
MDJ11103 - Computer Programming Laboratory Module

i. What p is pointing to in line 15?


__________________________________________________________________________

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;

//pointers operation on array


printf(“\nPointers operation on array:\n”);
arrPtr = array;
for(i=0;i<SIZE;i++)
printf("arrayPtr points to array[%d] = %d\n", i,*(arrPtr + i));

//regular operation on array


printf(“\nRegular operation on array:\n”);
for(j=0;j<SIZE;j++)
printf("array [%d] = %d\n", j, array[j]);

return 0;
}

a. Write the output of the program.

5
MDJ11103 - Computer Programming Laboratory Module

b. Write a program to:


i. Read Test 1 and Test 2 marks and store in array test1 and array test2 where each
array stores 5 elements.
ii. Total up marks from test1 and test2 arrays and store in array named total.
iii. Print the total marks from array total.

Use pointers to access array members.

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

Total [0] = 130


Total [1] = 100
Total [2] = 100
Total [3] = 140
Total [4] = 145

Use the following segment to start your program.


#include <stdio.h>

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;

//read values and store into test1 and test2


//print contents array test1 and test2 //sum test1 and test2 and store in array 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>

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

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;
*pv = 6;
printf("\nWithin funct2:\t u = %d v = %d", *pu, *pv);
}

7
MDJ11103 - Computer Programming Laboratory Module

a. Write the output of the program.

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.

You are required to write the following functions:


a) getData - This function reads two numbers into variables. In the main function, the
parameters should be passed by reference.
b) divide - This function divides two integers. In the main function, the output
parameters should be passed by reference.
c) print - This function prints the quotient and the remainder.

Sample Output:

Enter two integers : 13 2


Quotient :6
Remainder :1

8
MDJ11103 - Computer Programming Laboratory Module

3.5 Additional Exercise (Optional)


Write a program to input marks, sort and display the sorted marks. Use the following function
prototypes:-

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

Marks in ascending order = 12 34 45 55 66

You might also like