You are on page 1of 11

LAB MODULE 6

POINTERS

ASSOCIATE PROFESSOR ABDUL RAHMAN BIN MOHD SAAD


MOHAMAD RIZAL BIN ABDUL REJAB
ROHANI S MOHAMED FAROOK

Electronic Department
Faculty of Engineering Technology
Department of Electronic Engineering Technology
Universiti Malaysia Perlis

1
Objectives:
1. Ability to understand pointers and pointers operators.
2. Ability to define and initialize pointers.
3. Ability to understand the relationships among pointers, arrays and strings.
4. Ability to use pointers to pass arguments to function by reference.

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.

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 = #

Pointer Operators

Symbol & is called the address operator.

1. It Returns the address of the operand.


int num = 7;
int *numPtr;
numPtr = # //numPtr gets address of num
//numPtr“pointsto”num

2
2. 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 to10

 Dereferenced pointer (operand of *) must be an value (left values, can be


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

Sample Program

Given the following programs, trace the different variables and pointers as they are
changed. (Note: %p is used to display data type pointer (address))

1. Sample Program 1

/* Lab Module 6 sample 1 code pointers


mohamad rizal abdul rejab
m.rizal@unimap.edu.my
+60194461766
*/

#include<stdio.h>
int main()
{

int a;
int *aPtr; // aPtr is pointer to an integer

a = 7;
aPtr = &a; // aPtr sets to address of a

printf("\n The value of a is %d "


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

printf("\n The address of a is %p"


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

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


*aPtr=15;

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


printf("\n Showing that * and & are complements of each other \n");

3
printf("&*aPtr = %p\n",&*aPtr);
printf("*&aPtr = %p\n",*&aPtr);

return 0;
}

Output of the program

The value of a is 7
The value of *aPtr is 7
The address of a is 000000000023FE5C
The value of aPtr is 000000000023FE5C
a=7 *aPtr = 7 &a=000000000023FE5C aPtr =
000000000023FE5C

Dereferencing pointer, *aPtr =15 and a=15

Showing that * and & are complements of each other


&*aPtr = 000000000023FE5C
*&aPtr = 000000000023FE5C

2. The following program demonstrates the relationship between pointers and


arrays.
/* Lab Module 6 sample 2 code pointers
mohamad rizal abdul rejab
m.rizal@unimap.edu.my
+60194461766
*/

#include<stdio.h>
#define SIZE 5

int main ()
{
int array[SIZE] = {5,11,23};
int *arrPtr;
int i,j;

//pointers operation on array


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

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

return 0;
}

Output program
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

5
TASKS

1. Declare the following pointer variables:

a. A pointer variable pfVar1 pointing to a float

b. A pointer variable piNum pointing to an int

c. A pointer variable pcChar1 pointing to a char

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

a. Declare the variable piNum1 to be a pointer to an object of type int.

b. Assign the address of variable iValue1 to pointer variable piNum1.

c. Print the value of the object pointed to by piNum1.

3. Determine output for each of these program segments:


a.
int num = 11;
int *ptr;
ptr = &num;
printf(“ %d \t %d \n”, num,*ptr);

6
b.
floatval = 4.50;
float *vPtr = &val;
*vPtr *= 5;
printf(“ %.2f ”, *vPtr);

c.
float *nomPtr;
float nom = 3.25;
nomPtr = &nom;
*nomPtr = 4.25 + *nomPtr*5;
printf(“%.2f ”, *nomPtr);

4. Trace the following program and answer the questions.

#include<stdio.h>
intmain(void)

{ //variablesandpointersdeclarations
int a;
int b;
int c;
int *p;
int *q;
int *r;

a = 6;
b = 2;
p = &b; //--------------------------------------------->line15

q = p; //------------------------------------------------>line17
r = &c; //------------------------------------------------>line18

p = &a; //------------------------------------------------>line20
*q=8; //------------------------------------------------>line21

*r = *p; //------------------------------------------------>line23
*r = a + *q + *&c; //------------------------------------------------>line25

printf("\n a = %d \t =%d \t c = %d \n", a,b,c); //---------------------------->line27


printf(" *p = %d \t *q = %d \t *r = %d\n", *p,*q,*r); //--------------------------->line28
printf(" &a =%p \t &b = %p \t &c =%p \n", &a, &b, &c);//------------------------->line29
printf(" p=%p\t q=%p\t r=%p\n", p, q, r); //------------------------------->line30

7
return 0;
}

a. What p is pointing to in line15?

b. Which variables are pointed by q and r in lines 17 and lines 18? Give the
value of the variable pointed by q.

c. Referring to lines 25, is the content of variable c changing? If yes, what is


the value of c. If no, state your reason.

d. Display the outputs in lines 27, 28, 29 and 30 on the screen.

8
5. 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)
intmain()
{
int u = 0;
int v = 0;

printf("\n Before calling funct1: = %d v = %d",u,v);


funct1(u,v);
printf("\n After calling funct1: u = %d v = %d",u,v);

printf("\n\n Before calling funct2: u = %d v = %d",u,v);


funct2(&u, &v);
printf("\n After calling funct2: u = %d v = %d \n", u,v);
return 0;
}

void funct1(int u, int v)


{
u = 2;
v = 6;
printf("\nWithinfunct1:\t = %d v = %d",u,v);
}

void funct2(int *pu, int *pv)


{
*pu = 2;
*pv = 6;
printf("\nWithin funct2: \t = %d v = %d",*pu, *pv);
}

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.

9
6. Write a program that reads the values for the width, length, and height of a
rectangular box using three variables of double type in the function main( ). Pass
values of these variables to a function, which calculates the volume and surface
area for six sides of the box. The volume and surface area of the box passed
back from two other arguments of this function are printed out in the function
main ( ). Check your program with the user input for the width, length, and height
of 2, 3, 4 meter, respectively.

7. Write a program to:


a. Read Test1 and Test2 marks and store in array test1 and array test2
where each array stores 5 elements.
b. Total up marks from test1 and test2 arrays and store in array named
total.
c. Print the total marks from array total.

Use pointers to access array members.


Sample Output:
Enter Test1 and Test2 marks: 50 80
Enter Test1 and Test2 marks: 40 60
Enter Test1 and Test2 marks: 30 70
Enter Test1 and Test2 marks: 84 56
Enter Test1 and Test2 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

Sample of C Programming
#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;

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

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

9. Write 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

11

You might also like