You are on page 1of 5

Computer Programming Laboratory, Spring 2017

COMPUTER PROGRAMMING LABORATORY


Experiment # 1:
Pointers I

OBJECTIVES

The main purpose of the experiment is to introduce you to C pointers. In this experiment,
firstly, definition and initialization of a pointer is explained. Then, pointer operators are
introduced. Lastly, function call by reference concept is analyzed and some examples are
studied.

INFORMATION

Pointers, Definitions and Initialization

Pointers are variables such as int, double, and char, but pointers store memory addresses
instead of values. Generally, a variable as in int, double, and char type contains a specific value
according to their type. On the other hand, pointers contain memory addresses that point a
specific value. Therefore, reaching a value through a pointer is called indirect reference. Fig. 1
shows a pointer example. Consider c is a variable as in char type and it contains A. In the
second statement, a pointer definition is given. The *cptr is a pointer to a char variable.
Initializing the cptr pointer is established at the last statement. At that point, we could reach A
value by using variable name c directly or by using pointer name *cptr indirectly.

Fig. 1 A Pointer Example [1].

Pointers could be defined as follows:

int *burakPtr, burak, *kaleciPtr, kaleci;

the variables *burakPtr and *kaleciPtr are in pointer type. As seen from Fig. 1 and the above
definitions asterisk * is used to declare pointer variables, and each pointer must be declared
with *prefixed to the name. Additionally, including the postfix Ptr to a pointer variable name
emphasizes that the variable stores a memory address and it is a pointer variable. On the other
hand, burak and kaleci variables are defined to be int, not a pointer. Pointers must be initialized
after their definition. A pointer may be initialized to NULL or an address.

Pointer Operators

There are two pointer operators. One of them is address operator which is indicated with &.
The operator returns the address of its operand [2]. Consider the following statements:

double b=3.7;
double *bPtr;
bPtr=&b;

E-mail : burakaleci@gmail.com
Computer Programming Laboratory, Spring 2017

b is a double variable and bPtr is a pointer that points a double variable. In the last row, the
address of b assigns bPtr.

Second pointer operator generally known as the indirection or dereferencing operator and it is
represented with *. The operator returns the value of the variable to which its operand [2].
Assume that x is an integer variable and xPtr points to x as follows:

int x=7;
int *xPtr;
xPtr=&x;
int temp=(*xPtr);

after executing the statements temp variables contains 7 because (*xPtr) returns the value of the
variable at location xPtr, and also &x, and it means that 7.

It is important to note that, the & and * operators are complements of one another when
they are both applied consecutively to a value variable or a pointer variable, the variable remains
same because they cancel each other. The description of the pointer operators is given in Fig. 2.

Fig. 2 Description of the pointer operators

An example code for pointer operators is given in Fig. 3. Line 20 and 21 print value and address
of the b variable, respectively. Line 23 and 24 print bPtr (same as address of b) and *bPtr (same
as value of b). In line 26, pointer operators cancel each other and the value of the b is printed. In
line 29 and 30, in similar manner pointer operators cancel each other and the value of the bPtr
(same as address of b) is printed. Result of the pointer operator code is given in Fig. 4.

Fig. 3 An example code for pointer operators.

E-mail : burakaleci@gmail.com
Computer Programming Laboratory, Spring 2017

Fig. 4 The result of pointer operators code

Passing Arguments to Functions by Reference

There are two ways to pass arguments to a function: call-by-value and call-by-reference. In
call-by-value method, the copy of the value is created and is used in the function. After the
function finishes its task one value may be returned to caller function. On the other hand, many
functions require the capability to modify one or more variables in the caller or to pass a pointer
to a large data to avoid the overhead of passing by value [2].

Pointers and pointer operators & and * enable us to pass arguments to function call-by-
reference. In this method, the address of the variable passes the function by using address
operator (&). The original variable is used in the function and its value will be modified. When
address of a variable is passed to a function, the dereferencing operator (*) must be used in the
function to modify the value at that location in the callers memory [2].

An example code to examine call-by-value and call-by-reference methods is given in Fig. 5.


Assume that we have a mobile robot and its initial pose is zero. Also, it opearates constant linear
velocity 200mm/sec. The purpose is calculating its next pose after 1 second is elapsed. The
variables pose, velocity and interval are printed before and after calculateNextPose function call.
It is important to note that only pose variable is passed to function call-by-reference method and
only its value is modified after function. The variables velocity and interval are passed to
function call-by-value and their values remains same in the caller function main. Result of code
is given in Fig. 6.

Fig. 5 An example code for call-by-value and call-by-reference.

E-mail : burakaleci@gmail.com
Computer Programming Laboratory, Spring 2017

Fig. 6 The result of call-by-value and call-by-reference code

QUESTIONS

1) Write and run the following code. Fill the blanks.

The value of c is ---------------------------------


The value of &a is ------------------------------

The value of bPtr is -----------------------------


The value of *bPtr is ----------------------------

The value of cPtr is -----------------------------


The value of *aPtr is ----------------------------

The value of *&a is -----------------------------


The value of *&c is -----------------------------

The value of *&aPtr is -------------------------


The value of &*bPtr is -------------------------
The value of &*cPtr is -------------------------

Write down your solutions on a clear


white A4 size paper and give it to the
instructor before laboratory hours.
Discuss overall results. Give a conclusion
about the results.

2) Write a C program to calculate the equation as follows:

= 2 + +

where a, b, and c are constant. Assign a, b, c, and x values randomly. Print the variables x, a, b,
and c before polinomial function call. Then, use polinomial function to calculate result of the
equation. Print the variables x, a, b, and c after polinomial function call (Hint: Only x value must
be modified after function call).

3) Write a C program to calculate rotating coordinates by using following formula.

= cos
= sin +

Prompt the current coordinates ( , ), and rotation angle . Print the current coordinates,
rotation angle and rotating coordinates before rotate function call. Then, use rotate function to
calculate rotating coordinates ( , ). Print ( , ), and ( , ) after rotate function call.
1
Test your program with [ ] = [ ] and =90 degree (Hint: Only ( , ) values must be
2
modified after function call).

E-mail : burakaleci@gmail.com
Computer Programming Laboratory, Spring 2017

4) Write a C program to convert Cartesian coordinates (x, y, z) to spherical coordinate (r, , )


system. Prompt the (x, y, z) values. Print (x, y, z) and (r, , ) before convert function call. Then,
use convert function to calculate spherical coordinates (r, , ) according to the following
formulas:

= 2 + 2 + 2
2 + 2
= 1

1

=

Print (x, y, z) and (r, , ) after convert function call. Test your program with x=-2, y=6, and
z=3. (Hint: Use atan2 function. atan2 (double y, double x) and returns arc tan of y/x. Only
(r, , ) values must be modified after function call).

5) Write a C program to calculate natural response of an RC circuit. Prompt the R, C, and Vo


values from the user and print them before response function call. Then, use response function to
calculate the capacitor voltages as following:

() = 0 , 0.

For each t instance, print R, C, Vo, and t after response function call. Test your program with
R=1k, C=0.01F and Vo=12V at t=10, t=20, t=30, t=40 and t=50. (Hint: use exp function)

[1] http://www.roseindia.net/tutorial/datastructure/pointer.html, 2017.


[2] P. J. Deitel and H. M. Deitel, C How To Program Fifth Edition, Prentice Hall, 2007.

E-mail : burakaleci@gmail.com

You might also like