0% found this document useful (0 votes)
232 views6 pages

UTech CMP1025 Tutorial Lab #6 - Pointers

This lecture covers pointers in C programming. It includes examples of pointer arithmetic and dereferencing pointers. There are also exercises on defining pointers, modifying values using pointers, and writing functions that accept and return pointer parameters. The lecture concludes with two case studies on using pointers to read and write data to files, and search for values in arrays.

Uploaded by

Leia Michaelson
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
232 views6 pages

UTech CMP1025 Tutorial Lab #6 - Pointers

This lecture covers pointers in C programming. It includes examples of pointer arithmetic and dereferencing pointers. There are also exercises on defining pointers, modifying values using pointers, and writing functions that accept and return pointer parameters. The lecture concludes with two case studies on using pointers to read and write data to files, and search for values in arrays.

Uploaded by

Leia Michaelson
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Department: School of Computing & IT (SCIT)

Course: Programming 2 (CMP1025)


Duration: Sem. 2 AY 2016-17

Lecture #6 – Pointers
=============================================

Lab and Tutorial Exercises:


1. Rewrite each of the following expressions by replacing the indirection
operator (*) with the index operator ([..]). Each identifier refers to an
array.
a). *(tax + 4) _____________________________________
b) *(score + 2) ___________________________________
c) *(number + 10) ___________________________________
d) *prices ___________________________________

2. Rewrite each of the following expression by replacing the index operator


([…]) with the indirection operator (*).
a. tax[6] ____________________________________
b. score[7] ____________________________________
c. num[4] ____________________________________
d. prices[9] ____________________________________

3. Find the error in each of the following program segments. Assume


int *zPtr; // zPtr will reference array z
int *aPtr = NULL;
void *sPtr = NULL;
int number, i;
int z[ 5 ] = { 1, 2, 3, 4, 5 };
sPtr = z;
a) ++zptr;
b) // use pointer to get first value of array; assume zPtr is initialized
number = zPtr;
c) // assign array element 2 (the value 3) to number;
assume zPtr is initialized
number = *zPtr[ 2 ];
d) // print entire array z; assume zPtr is initialized

CMP1025: Lecture #6 – Pointers Page 1 of 6


for ( i = 0; i <= 5; ++i ) {
printf( "%d ", zPtr[ i ] );
}
e) // assign the value pointed to by sPtr to number
number = *sPtr;
f) ++z;

4. State the values that would be printed in the code fragment below.
int x = 5, y;
int *Ptr;
Ptr = &x;
y = *Ptr;
*Ptr = *Ptr + 3;
printf("%d %d %d\n", x, y, *Ptr);
y = *Ptr + 1;
(*Ptr)++;
printf("%d %d\n",y,*Ptr);

5. Given the following declarations, show the contents of memory after each
set of statements is executed.

int i = 3, q = 9, *f = NULL, *s = NULL;


float c = .5, *j = NULL;

6. Answer each of the following. Assume that unsigned integers are stored in

CMP1025: Lecture #6 – Pointers Page 2 of 6


2 bytes and that the starting address of the array is at location 1002500
in memory.
a) Define an array of type unsigned int called values with five
elements, and initialize the elements to the even integers from 2 to
10. Assume the symbolic constant SIZE has been defined as 5.
b) Define a pointer vPtr that points to an object of type unsigned int.
c) Print the elements of array values using array subscript notation.
Use a for statement and assume integer control variable i has been
defined.
d) Give two separate statements that assign the starting address of
array values to pointer variable vPtr.
e) Print the elements of array values using pointer/offset notation.
f) Print the elements of array values using pointer/offset notation with
the array name as the pointer.
g) Print the elements of array values by subscripting the pointer to the
array.
h) Refer to element 5 of array values using array subscript notation,
pointer/offset notation with the array name as the pointer, pointer
subscript notation, and pointer/offset notation.
i) What address is referenced by vPtr + 3? What value is stored at
that location?
j) Assuming vPtr points to values[4], what address is referenced by
vPtr -=4? What value is stored at that location?

7. Declare and array of string pointers to store the months of the year.
Ensure that each month is stored at an index. Print the months from the
array.
8. Write a function that accepts a string (a pointer to a character) and
deletes the last character in the string.
9. Write a function that accepts a string (a pointer to a character) and
deletes the first character in the string.
10.Write a function that accepts a pointer to a string and a character and
returns the number of times the character is found in the string.
11.Write a function that accepts a pointer to a string returns the number of
vowels found in the string.
12.Write your own string length, string copy, string compare and string
concatenate functions.
13.Write the function prototype and header for a function ProcessValues()
that accepts an integer pointer iPtr as an argument.

CMP1025: Lecture #6 – Pointers Page 3 of 6


14.Write the function prototype and definition for a function Display() that
accepts two arguments a floating-point pointer aPtr and the size of the
array as integer. When executed the program displays the contents of the
array, using pointer offset notations. The function returns nothing.

15.Write the function prototype and header for a function ModifyValues()


that accepts no argument but returns an integer pointer.

16.Write the function prototype and definition for a function GetName() that
accepts no argument but returns a string pointer. When executed the
program will prompt the user for a name and then returns the input to the
caller.

17.Using modularity and pointers re-write the program that will:


a. Declare and initialize (in main) a 10-element array called LIST with
the following numbers: {-12, -6, -4, 5, -8, -15, -3, -10, -22, 15}.
b. Create a function that uses pointer to determine and output the
smallest number in the array
c. Create a function that uses pointer to determine and output the
largest number in the array
d. Create a function that uses pointer to determine and output the
sum and average of the values in the array.

18.Using modularity and pointers, re-write a program that will:


a. Read 5 numbers (from 1 – 20) into an array N.
b. Output the information in the form of a histogram where for each
number entered, a bar consisting of that many asterisks is printed
c. Modify part (a) to accept random number from 1-25 into array N

See the sample output below:

Element Value Histogram


0 20 ********************
1 3 ***
2 6 ******
3 8 ********
4 11 ***********

===============================================

CMP1025: Lecture #6 – Pointers Page 4 of 6


CASE STUDY:

1. Implement the following function definitions below:


a) Write the function definition for a function call findSmallest(), that
accepts two (2) arguments: a floating-point pointer called vPtr,
and integer variable size. Using the pointer vPtr, this function
searches through all the elements in the array and finds the
smallest number. The function returns the index of the smallest
number.
b) Write the function definition for a function call printListOfValues(),
that accepts two (2) arguments: a floating-point array values[],
and an integer variable size. When executed this function must
display all the elements in the array in a neat tabular format. The
function returns nothing.
c) Write the function definition for findDeviation(), that accepts
three (3) arguments: two (2) floating-point pointers
benchmarkPtr and currentPtr, along with the integer variable
size. This function must declare a local floating-point array called
deviation, size 5. When executed, this function must subtract the
value at each index of benchmark from current arrays (using
benchmarkPtr and currentPtr respectively) and store the result
at the corresponding index in the deviation array. The function
returns the deviation array.
d) Implement a main module:
i. so that given a list of numbers (user input – stored in
current[] array), the program will calculate the deviation
between the current[] array and the benchmark[] array
(default value 5.6, 9.3, 5.1, 4.0, 2.0) values at each index.
ii. to find and display the smallest number in the deviation[]
array using pointer.
iii. to display all the values in the benchmark[] array.
e) **The user chooses when to exit the program.
f) Bonus: Write a function definition for a function called
WriteDeviationToFile(),that accepts three (3) arguments: two
(2) floating-point pointers, benchmarkPtr and currentPtr; along
with the integer variable size. When executed the program writes
the benchmark and current array data to a file called
“deviation.txt”.

2. Using modularity, enum, files, arrays and pointers, write a C program


that creates a list of students in a class, and store their details (formatted

CMP1025: Lecture #6 – Pointers Page 5 of 6


data) in a file called, students.txt. The program should add each new set
of data to the end of the file.
a) When executed the program prompts the user for the following
data: ID number (int), student’ name(string), tuition(float),
paid(bool).
Bonus: Allow the user to enter details until he/she chooses to stop.
N.B. Open the file on your hard drive to ensure it was updated
correctly.

b) Allow the user to search for all the students with paid tuition, and
read their names from the file and store these into a string array
(ie array of char pointers) then print the names from the array to
the monitor.

c) The program must also calculate the total amount of tuitions


received so far and display this to the monitor.

d) Allow the user to search for a specific student details using the
student’s ID number. Print the details of the student if found,
otherwise print a message to say, “File record not found for ID#
100.

e) Bonus: Allow the program to prompt the user for the file name.
Allow the user to delete and rename a file (library functions may
exist for these).

CMP1025: Lecture #6 – Pointers Page 6 of 6

You might also like