Pointers
Pointers
▪ Since the value of P is the address of variable quantity, we may access the value of quantity by using the value of
P.
▪ We may say, the variable P points to the variable quantity.
▪ Thus P is called “Pointer”. (We are not actually concerned about the actual values of the pointer variables. This
might be different everytime we run the program.)
▪ We are concerned about the relationship between the variables P and quantity.
If x is an array, the pointing to an element
Underlying Concepts of Pointers of the array &x[0] or &x[i+3] is valid.
❑ Memory addresses within a computer are referred to as pointer constants. We can not change
them, we only can use them to store data values.
❑ We can not the save the value of the memory address directly. We can only obtain the value
through the variable stored there using the address operator (&). The value thus obtained is
known as pointer value. The pointer value (the address of a variable) may change from one run
of the program to another.
❑ Once we have a pointer value, it can be stored into another variable. The variable that contains a
pointer value is called pointer variable.
#include<stdio.h>
int main(void)
{ printf("%c is stored at the address %u.\n", a, &a);
char a; printf("%d is stored at the address %u.\n", x, &x);
int x; printf("%f is stored at the address %u.\n", p, &p);
float p, q; printf("%f is stored at the address %u.\n", q, &q);
return 0;
a = 'A'; }
x = 2;
p = 4.5, q=7.4;
Declaring a Pointer
❑ Since pointer variables contain memory addresses that belong to a separate data type, thye must
declared as pointers before we use them.
❑ The declaration of a pointer variable takes the following form :
int x, *p, y;
data_type *pt_name; x = 10;
▪ The asterisk (*) tells that variable pt_name is a pointer variable. p = &x;
▪ pt_name needs a memory lcation. y= *p; / accessing x through p/
▪ pt_name points to a variable of type data_type. *p = 20; / assigning 20 to x /
Example : int *p;
[The variable p acts as a pointer variable that points to an integer data type.
The type int refers to the data type of the variable being pointed to by p and not the
type of the value of the pointer]
Example : float *x declares x as a pointer to floating-point variable.
Variable Value
p ??? Points to unknown location
Garbage Value
Initialization of Pointer Variables
▪ It is important to initialize pointer variables carefully before they are used in the program.
▪ Once a pointer variable is declared, we can use the assignment operator to initialize the variable.
▪ We must ensure that the pointer variables always point to the corresponding type of data.
▪ We must ensure that the target variable is declared first and then the pointer variable is declared
and initialized.
▪ The first line declares quantity and n as integer variables and p as pointer variable pointing
to a integer variable.
▪ The second line assigns 179 to the variable quantity.
▪ The third line assigns the address of quantity to the pointer variable p.
▪ The forth line contains the indirectonal operator * : When the operator * is placed in front of
a pointer variable in an expression (on the right side of an equal sign), the pointer returns the
value of the variable of which the pointer value is the address. Thus the value of the n would
be 179.
Workout Problem 11.2.
x = *p1**p2-6;
#include<stdio.h>
y = 4*-*p2/ *p1 +10;