Pointers

You might also like

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

Understanding Pointers ▪ When we declare a variable, the system allocates,

somewhere in the memory, an appropriate space


to hold the value of the variables.
Memory Cell Address ▪ Since every byte has an unique address number,
0 this location will have its own address number.
▪ Consider the following statement :
1 int quantity = 179;
2
Quantity Variable
3
179 Value
4 5000 Address
….
▪ The above statement instructs the system to find
…. a location for the integer variable quantity and
…. puts the value 179 in that location.
▪ Let us assume that the system has chosen the
…. address location 5000 for the variable quantity.
65535 ▪ The address of a variable is the first byte
occupied by the variable.
Understanding Pointers
▪ During the execution of the program, the
system always associates the name quantity
with address 5000.
Variable Value Address ▪ We may have the access to the value 179
quantity 179 5000 either by the name quantity or the address
5000.
▪ Since memory address are simply numbers,
they can be assigned to some variables, that
can be stored in memory, like any other
P 5000 5048 variables.
▪ Such variables that hold memory addresses
are called pointer variables.

▪ 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.

The following are illegal use of address operator:


int quantity = 2; 1. &125 (pointing at constants)
int *P; 2. int x[10];
P = &quantity; &x; (pointing at array names)
3. &(x+y)(pointing at expressions)
Workout Problem 11.1.

#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.

int quantity; int quantity; float a, b; int x, *p=&x; /*valid*/


int *p; int *p = &quantity; int x, *p; int *p=&x, x; /*invalid*/
p = &quantity; p = &a; /*wrong*/
b = *p; int *p=0; /*valid*/
int *p=NULL; /*valid*/
int *p=5360; /*invalid*/
Accessing A Variable through Pointers

int quantity, *p, n;


Quantity = 179;
p = &quantity;
n = *p;

▪ 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.

printf("The value of x is : %d \n\n", x);


printf("%d is stored at the address %u.\n\n", x, &x);
#include<stdio.h> printf("%d is stored at the address %u.\n\n", *&x, &x);
int main(void) printf("%d is stored at the address %u.\n\n", *ptr, ptr);
{ printf("%d is stored at the address %u.\n\n", ptr, &ptr);
int x, y; printf("%d is stored at the address %u.\n\n", y, &y);
int *ptr;
x = 10; *ptr = 25;
ptr=&x; printf("%d is stored at the address %u.\n\n", *ptr, ptr);
y = *ptr; return 0;
}
Workout Problem 11.3.

x = *p1**p2-6;
#include<stdio.h>
y = 4*-*p2/ *p1 +10;

int main(void) printf("a = %d, b = %d\n\n", a, b);


{ printf("x = %d, y = %d\n\n", x, y);
int a, b, *p1, *p2, x, y, z;
a = 12; *p2 = *p2 + 3;
b = 4; *p1 = *p2 - 5;
p1 = &a; z = *p1**p2-6;
p2 = &b;
printf("a = %d, b = %d\n\n", a, b);
printf("z = %d\n\n", z);
printf("Address of a : %u.\n", p1);
return 0;
printf("Address of b : %u.\n\n", p2); }

You might also like