Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

CIS 1101 Exercise: Variable and Pointer Variable

April 20, 2020

A. Answer each of the following questions in 1 complete sentence.


1) What is a variable?

2) What is a pointer variable?

3) What is the difference between a character variable and a character pointer variable ?

4) What is the difference between an integer variable and an integer pointer variable ?

5) What does dereference or indirection mean? What is the symbol for dereference operator in C?

B. Run this program in any C compiler and answer the following questions below.
#include <stdio.h>

int main()
{
char x = 'A';
int y = 10;

char * xptr;
int * yptr;

printf("\nVariable x: size = %d, value = %-5c and address is %p", sizeof(x), x, &x);
printf("\nVariable y: size = %d, value = %-5d and address is %p", sizeof(y), y, &y);

/*** Assign the address of x and y to xptr and yptr respectively ***/
xptr = &x;
yptr = &y;

printf("\nVariable xptr: size = %d, value = %p and address is %p", sizeof(xptr), xptr, &xptr);
printf("\nVariable yptr: size = %d, value = %p and address is %p", sizeof(yptr), yptr, &yptr);

return 0;
}

1) Answer the question and complete the table below based on the program above and its output.

What is the C compiler and version used to execute the program?

Variable Data type Size (in bytes) Value Address


x

y
xptr

yptr
2) Represent each variable above (x, y, xptr and yptr) by a box, whose box size is proportional to the variable’s byte
size. That is, a 4-byte variable has a box size that is 4 times bigger than a 1-byte variable. Label each box with the
variable’s name, value and beginning address. If the variable’s value is an address of another variable, draw an
arrow from the box’s value to the other box that has that address.

C. Determine the symbols of the operators used in each of the following and give a sample C statement using the
operator.

Operator Sample C Statement Description of the Operator


1. The operator used to declare a pointer variable.
2. The operator used to assign the address of a variable to a pointer variable.
3. The operator used to dereference
4. In the function header, the operator used to pass a scalar variable to a function by
address.
5. In the function call, the operator used to pass the address of the scalar variable.

D. Answer the following:

1. Write the declaration of the following variables. Write the answers on the space provided for.
a) X and Y - as pointers to an integer variable
b) Z – as an integer variable
2. Using the declaration in #1, write the ONE (1) C statement on the space that will accomplish each of the following:
a) Let X point to Z.
b) Using X, let Y point to Z.
c) Input from the keyboard the value of Z.
d) Using X, display on screen the value of Z.
e) Using Y, change the value of Z to 100.
f) Display on screen the address of Z in hexadecimal form.
g) Display on screen the value of Y in hexadecimal form.

You might also like