0% found this document useful (0 votes)
21 views

CSC 1401 Lab Manual (Lab 9)

The document discusses pointers in C programming. It explains that pointers allow a function to directly access and modify variables in another function's scope by passing their addresses rather than values. This is known as call by reference. The document provides examples to illustrate the difference between call by value and call by reference. It also presents sample problems for readers to practice pointers and passing arguments by reference.

Uploaded by

Hiba Ait ijjou
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

CSC 1401 Lab Manual (Lab 9)

The document discusses pointers in C programming. It explains that pointers allow a function to directly access and modify variables in another function's scope by passing their addresses rather than values. This is known as call by reference. The document provides examples to illustrate the difference between call by value and call by reference. It also presents sample problems for readers to practice pointers and passing arguments by reference.

Uploaded by

Hiba Ait ijjou
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

School of Science and Engineering Computer Science Department

Lab 9
C Programming
Pointers

1
School of Science and Engineering Computer Science Department

I. Introduction:

In the previous chapter, you learned how to write a C program using different functions.
You decomposed the program into different tasks such that each task is accomplished using a
separate function. You also learned how to provide input arguments to a function and how to
return a single value from a function to the calling function.

In order to understand the topic of this lab, we need to learn about function scopes. Every
function that you define in your C program hasits own scope/interval in memory. The variables
declared inside a function (i.e., local variables), can be used only by statements inside that
function or block of code. This triggers the following question:
- What happens when we send an input argument from a function to another function?
Can the called function access the memory cell of that argument in the scope of the
calling function?

The answer to this question depends on how the function called. In fact, there are two types of
function calls that differ in terms of how the input arguments are sent: call by value, and call by
reference.
1. Call by value

The type of function calls you have been working with so far is called a “call by value”.
This refers to the idea that the input arguments sent to a function are sent by value. When an
argument is sent by value, a copy of it is created in the scope of the called function. Let us
consider a simple program that swaps the values of two numbers using a function swap() and
displays the new value of each number in the main function.

We might expect that this program will display the following: a = 10 b = 5; however, that it
not the case. The variables a and b were declared in the scope of the main function and
initialized to 5 and 10, respectively. When we sent the numbers a and b to the function swap(), a

2
School of Science and Engineering Computer Science Department

copy of each number is created in the scope of the function swap(); meaning, the values 5 and
10 get stored in new memory cells that belong to the scope of the function swap(). Therefore,
when we perform any operation on the numbers a and b in the swap() function, nothing happens
to the variables a and b stored in the scope of the main function. So, this program will simply
display a = 5 b = 10. This is a call by value.

2. Call by reference

A call by reference allows a function to have direct access to a memory cell in the scope of
another function, and thus perform changes on the variable stored in that memory cell. In a call
by reference, the address of an input argument is sent rather than its value. For example, if a
variable name is x, then &x gives us its address. The variables used to hold the address of other
variables are called pointers. If a variable holds the address of an integer variable, it is said to
be a pointer to an integer, and its data type is int *; same goes for other data types.

The swap() function discussed previously could not perform any changes on the variables a
and b declared in the main function because they were sent by value. Let us write the swap()
function that takes the input arguments a and b by reference:

Let us look at the function call first: swap(&a, &b). Since we want to give direct access
to the function swap() to change the values of a and b; we have to send their addresses in
memory and not simply their values. Now, let us look at the function prototype. Since the
function swap takes the address of a and the address of b, this means that it takes two pointers
to two integers; therefore, the data types of its arguments are int*. Let us move now to the
function definition, you can see that we used *a, and *b instead of a and b; this is called de-
referencing a pointer, in other words, accessing the value stored in the address pointed by that
pointer. Assume that a is stored in the address 500, and b is stored in the address 504; then in the
function swap before doing the swap operation, a refers to the address 500 and *a refers to the
value 5; similarly, b refers to the address 504 and *b refers to the value 10.

3
School of Science and Engineering Computer Science Department

II. Objectives:

 To learn about function scopes.


 To learn about pointers and their usefulness in C programming.
 To understand the difference between call-by-value and call-by-reference.

III. Reference to the Lecture:

Chapter on C programming.

IV. Solved Example:

Problem 1:

Write a C program that asks the user for their current account balance int the main and
uses a void function withdrawal() to allow the user to withdraw an amount of money
from their account. Your program should output the new balance after the withdrawal
operation.

Problems for Practice:

1. What is the output of the following segment of C code?


int a = 5;
int *ptr ;
ptr = &a;
*ptr = *ptr * 3;
printf("%d", a);
2. What is the output of the following C program?
#include <stdio.h>
void mul(int a, int b, int *c);
int main(void){
int x = 5, y = 2, z;
mul(x, y, &z);
printf(“ %d %d %d \n ”, x, y, z) ;
mul(y, x, &z);
printf(“ %d %d %d \n ”, x, y, z) ;
mul(z, y, &x);
printf(“ %d %d %d \n ”, x, y, z) ;
return 0;
}
void mul(int a, int b, int *c){
*c = a*b;
}

4
School of Science and Engineering Computer Science Department

3. Write a C program that allows an employer to promote the employees based on the
number of sales they reached. In the main function, the program starts by asking the
employer for the number of employees, then for each employee, the employer should
specify the old salary and the number of sales reached. The promotion of the salary
should be done by a void function that uses the following rules for promotion: if the
number of sales is less than 100, the salary increases by 500; if the number of sales is
between 100 and 500, the salary increases by 1000; otherwise, if the number of sales is
greater than 500, the salary increases by 2000. The new salary of each employee should
be displayed in the main function.

You might also like