Computer Programming Laboratory Experiment # 1: Pointers I Objectives
Computer Programming Laboratory Experiment # 1: Pointers I Objectives
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 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.
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 : [email protected]
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.
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.
E-mail : [email protected]
Computer Programming Laboratory, Spring 2017
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].
E-mail : [email protected]
Computer Programming Laboratory, Spring 2017
QUESTIONS
= 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).
= 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 : [email protected]
Computer Programming Laboratory, Spring 2017
= 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).
() = 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)
E-mail : [email protected]