Lab 1 Pointers
Lab 1 Pointers
BS CS Program
Introduction
Computer Memory
Each variable is assigned a memory slot (the size depends on the data type) and the variable’s
data is stored there
Pointers:
Pointers are a type of variable that allow you to specify the address of a variable. They
provide a convenient means of passing arguments to functions and for referring to more
complex data types such as structures. You need to declare and initialize pointers just as you
would other variables, but there are special operators that you need to use.
Address Operator (&): The "address of " operator (&) gives the memory address of the
variable.
The * Operator Dereference: The star operator (*) dereferences a pointer. The * is a
unary operator which goes to the left of the pointer it dereferences. The pointer must have a
pointee, or it's a runtime error.
Test Program:
void PointerTest() {
// allocate three integers and two pointers
int a = 1; int b = 2; int c = 3; int* p; int* q;
// Here is the state of memory at this point.
// T1 -- Notice that the pointers start out bad...
Pointer to Pointer
Types of Pointers
i) Null Pointer
A pointer should be set to zero when it is not assigned to a valid address. Such a pointer is
called a null pointer. Doing this will allow you to check whether the pointer can be safely
dereferenced, because a valid pointer will never be zero.
A void pointer is a pointer that has no associated data type with it. A void pointer can hold
address of any type and can be type casted to any type.
int a = 10;
char b = 'x';
void *p = &a; // void pointer holds address of int 'a'
p = &b; // void pointer holds address of char 'b'.
When we are accessing the value of a variable through pointer, sometimes we just need to
increment or decrement the value of variable though it or we may need to move the pointer to
next int position (just like we did above while working with arrays). The ++ operator is used
for this purpose. One of the examples of ++ operator we have seen above where we traversed
the array using pointer by incrementing the pointer value using ++ operator.
Pointer Arithmetic
pointer is an address which is a numeric value; therefore, you can perform arithmetic
operations on a pointer just as you can a numeric value. There are four arithmetic operators
that can be used on pointers: ++, --, +, and -
To understand pointer arithmetic, let us consider that ptr is an integer pointer which points to
the address 1000. Assuming 32-bit integers, let us perform the following arithmetic operation
on the pointer −
ptr++
the ptr will point to the location 1004 because each time ptr is incremented, it will point to the
next integer. This operation will move the pointer to next memory location without impacting
actual value at the memory location. If ptr points to a character whose address is 1000, then
above operation will point to the location 1001 because next character will be available at
1001.
Sample Program
Incrementing Pointer
Decrementing Pointer
Pointers as Strings
A string is a sequence of characters. A string type variable is declared in the same manner as
an array type variable is declared. This is because the string is an array of character type
variables.
The pointer variables can also be passed to functions as arguments. When a pointer variable
is passed to a function the address of the variable is passed to the function. Thus a variable is
passed to a function not by its value but by its reference.
Sample Program
Write a program to pass two parameters to the function to add a constant value of 100
to the passed values using pointers.
In the above program the function “temp” has two parameters which are pointers and are of
int type. When the function “temp” is called the addresses of the variables “a” and “b” are
passed to the function. In the function a value 100 is added to both variables “a” and “b”
through their pointers. That is the previous values of variables “a” and “b” are increased by
100. When the control returns to the program the value of variable a is 110 and that of
variable b is 120.
Practice Tasks
Task 2:
Task 3:
Write a program to input data into an array and find out the maximum value from array
through pointers.
Evaluation Criteria
The evaluation criteria for this lab will be based on the completion of the following tasks.
Each task is assigned the marks percentage which will be evaluated by the instructor in the
lab whether the student has finished the complete/partial task(s).
Evaluation of Lab
Outcomes:
-: Assignment Task :-
Write a program that dynamically allocates an array large enough to hold
a user defined number of test scores. Once all the scores are entered, the
array should be passed to a function that sorts them in descending order
and display the sorted list. Another function should be called that
calculates the average score and display in the same function. Use
appropriate headings for function and pointer notation rather than array
notation whenever possible.