Lab 10 - UniMAP Portal

EKT120: COMPUTER PROGRAMMING
LAB MODULE v1.1
LAB 10: INTRODUCTION TO POINTER
Pusat Pengajian Kejuruteraan Komputer Dan Perhubungan
Kolej Universiti Kejuruteraan Utara Malaysia
1
Objective:
1. Introduction
2. Pointer declaration
3. Pointer operator
1. Introduction:
Pointer is the address of an object (i.e. a specific memory location).
It can refer to different objects at different times
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
Contain memory addresses as their values
2. Pointer declaration
int *numPtr;
float *temperature;
char *nama;
Initialize pointers to 0, NULL, or an address
 int *nama = NULL;
OR
 int *numPtr = 0;
3. Pointer operator
Symbol & is called address operator
 Returns address of operand
 For example
int num = 7;
int *numPtr;
numPtr = # /* numPtr gets address of num */
numPtr “points to” num
Symbol * is called indirection/dereferencing operator
 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 */ show pictures!!
 Dereferenced pointer (operand of *) must be an lvalue (no constants)
2
Part A
Consider the declaration below:
int * ptrint
int * ptr1int;
int nom1;
Answer either True or False for the below statements.
1. ptrint = nom1; //Will get the address of nom1
Ans___________
2. ptr1int = &nom1; //ptr1int will point to nom1
Ans: __________
3. ptrint = ptr1int; // Will generate error during compilation
Ans: __________
4. *ptr1int = 20; // Will assign nom1 with 20
Ans: __________
5. printf (“%d”,*ptr1int);//Will print the value of 25 on the screen
Ans: __________
Part B
Task 1
The program below shows how to assign value and address to a pointer. Type, save and
compile the program below.
#include <stdio.h>
int main()
{
int u = 3;
int v;
int *pu;
int *pv;
pu = &u; //assign address of u to pu (referencing)
v = *pu; //assign value of pu to v
pv = &v; // assign address of v to pv
printf("\nu=%d
printf("\n\nv=%d
&u=%x
&v=%x
pu=%x *pu=%d", u, &u, pu, *pu);
pv=%x *pv=%d", v, &v, pv, *pv);
return 0;
}
Q1. Write down the output of the program.
3
Task 2
The program below shows how to access the content of a pointer and to change the value of a
variable via pointer. Type, save and compile the program below.
#include <stdio.h>
int main()
{
int u1, u2;
int v = 3;
int *pv;
u1 = 2 * (v + 5); //use value v
pv = &v;
u2 = 2 * (*pv + 5); //access *pv(dereferencing)
printf("\nu1 = %d
u2=%d", u1, u2);
*pv = 9; //change value v indirectly
printf("\n*pv=%d
v=%d", *pv, v);
return 0;
}
Q1. Write down the output of the program.
Q2. What is the difference between v and *pv? What happen to the value of v when *pv is
assign a new value?
4
Task 3
The program below shows the differences between a function that pass by value and a
function that pass by reference (pointer). Type, save and compile the program below.
#include <stdio.h>
void funct1(int u, int v);
void funct2(int *pu, int *pv);
void 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);
}
void funct1(int u, int v)
{
u = 2;
v = 6;
printf("\nWithin funct1:
}
void funct2(int *pu, int *pv)
{
*pu = 2;
*pv = 6;
printf("\nWithin funct1:
}
u=%d v=%d", u, v);
u=%d v=%d", *pu, *pv);
Q1. Write down the output of the program.
Q2. What is the value of u and v after calling func1? Is it different with the value of u and v
after calling funct2?
5
Task 4
The program below shows pointer operations on one dimensional array. Type, save and
compile the program below.
#include <stdio.h>
int main()
{
int array[5]={5,11,12};
int *arrPtr;
int i,j;
//pointers operations on array
printf(“Pointer operation on array\n”);
arrPtr = array; //to assign a pointer with the array address
for(i=0;i<5;i++)
printf("arrayPtr points to array[%d] = %d\n", i,*(arrPtr + i));
// *(arrPtr + i) => *arrPtr + elements index
//regular operations on array
printf(“Regular operation on array\n”);
for(j=0;j<5;j++)
printf("array [%d] = %d\n", j, array[j]);
return 0;
}
Q1. Write down the output of the program.
6
Part C
Write a complete C program to input marks, sorted the marks and print the marks. Use the
following function prototypes:void fillArray (int *, int); – to input marks into a 1 dimensional array
void printArray (int *, int); - to print the array elements
void swapArray(int *, int); - to swap the array elements
The sample output is shown below:Enter 5 marks:
45
66
12
34
55
Marks in ascending order =
..
12
34
45
55
66
7