Professional Documents
Culture Documents
POINTERS
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.
int *numPtr;
int *numPtr1, *numPtr2;
Pointer Operators
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
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
#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
3
printf("&*aPtr = %p\n",&*aPtr);
printf("*&aPtr = %p\n",*&aPtr);
return 0;
}
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
#include<stdio.h>
#define SIZE 5
int main ()
{
int array[SIZE] = {5,11,23};
int *arrPtr;
int i,j;
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
5
TASKS
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.
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);
#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
7
return 0;
}
b. Which variables are pointed by q and r in lines 17 and lines 18? Give the
value of the variable pointed by q.
8
5. The following program demonstrates the relationship between pointers and
functions (pass by reference).
#include<stdio.h>
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.
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;
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.
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