Professional Documents
Culture Documents
Management
C PROGRAMMING UNIT 5
Call-by-reference 2
A pointer is a derived data type in C. It is derived from other data types which are either
built-in or user defined.
We can easily check this memory address, using the & symbol.
If var is the name of the variable, then &var will give it's address.
#include<stdio.h>
int main() 5
{
int var = 7;
printf("Value of the variable var is: %d\n", var);
printf("Memory address of the variable var is: %x\n", &var);
/* %x prints address in hexadecimal number system, we can also use %d or %p. %p is
specifically the format specifier for pointers */
return 0;
}
Output: Value of the variable var is: 7
Memory address of the variable var is: bcc7a00
6
Pointers (also referred as pointer variables) are special variables in C
that hold address of memory locations, rather than some data used in
program.
Then using these pointers we can directly access the data stored at that
address.
Pointer Values: As users, we cannot set or assign the pointer address for memory
locations. Rather we can access the memory address value of variable using address
operator(&). This value is called Pointer Value. ( e.g. &quantity).
Pointer Variables: The Pointer values are stored in some special variables that are
tailored to store the memory addresses. They are known as Pointer Variables or
simple Pointers.
Accessing Address of Variables
11
Address of a pointer can be accessed using the address operator(&) on the variable name .
Eg: int quantity = 100;
&quantity will give the memory address allocated to variable quantity (i.e. place where
data 179 is stored.
Address operator can be applied only to variable or array name or even function name. It
is illegal to use address operator in the following ways.
1. &125 → applying & on a constant.
2. int x[100] , &x (applying & on a array name).
3. Address operator on at expressions like &(a+b).
Is x is an array , usage like &x[0] , &x[5] etc are legal. This will give addresses of the
first element and 6th element in the array respectively.
int main()
{
12
char a;
int x; OUTPUT:-
float p, q; A is stored at addr
3153847627.
a = 'A';
125 is stored at addr
x = 125;
3153847628.
p = 10.25, q = 18.76;
10.250000 is stored at addr
printf("%c is stored at addr %u.\n", a, &a); 3153847632.
printf("%d is stored at addr %u.\n", x, &x); 18.760000 is stored at addr
printf("%f is stored at addr %u.\n", p, &p); 3153847636.
printf("%f is stored at addr %u.\n", q, &q);
return 0;
}
Declaring Pointer Variables 13
Since pointer variables special variables that stores addresses than user data, there is a
slightly different syntax to declare them.
E.g int *p;
This declaration implies that p is a pointer variable that can store the address of integer type
variable.
General syntax for declaring a pointer variable is:
data_type *pt_name ;
This tells three things to compiler about variable pt_name.
1. The Asterisk(*) tells the variable pt_name is a pointer variable.
2. pt_name can point to memory location.
3. pt_name points to variable of type data_type.
Consider float *x;
This declares float type pointer variable which can store the address of a floating 14
point variable.
Pointer declaration Styles: Three styles are common for declaration of pointer
variables:
int* p; // style 1
int *p; // style 2
int * p; // style 3
Style 2 is recommended and more popular as it gives more clarity to the reader of the
code. E.g.
int *p,x,*q;
Initialization of Pointer Variables 15
So far we have declared pointer variables but did not make them point to any valid memory
location. Lets do that now so that the picture is complete.
int quantity = 179;
int *p; // Step 1. Declaring pointer
p = &quantity; // Step 2: Assigning value to pointer.
Alternatively, declaration and assigning can be done in single step also as:
int *p = &quantity;
16
Pointer Flexibility:
Pointer variables are variables in its very essence. Hence they need not bind it self to a 17
single data variable always.
Means a pointer variable can point different memory locations at different times.
E.G:
int x,y,z,*p; // p is pointer others integer variables.
. . p = &x;
. . p = &y;
. . p = &z;
This above usage is just fine. Note that, at any time, p points at only one variable.
At the same time, it is also possible that multiple pointers can point to same data
variable.
18
E.g:
int x;
int *p1 = &x;
int *p2 = &x;
int *p3 = &x;
This can be visualized as :
Accessing Variable Through its Pointer
19
Accessing the variable means accessing value stored in the variable.
An operator called Indirection operator(*) is used for that.
int x =100;
int *p = &x; // Creating a pointer to point to x.
printf(“%d”,x) // Outputs 100
printf(“%d”,*p) // Outputs 100
There is relation between pointers and arrays in C. When an array is declared (e.g. in
x[5]; ), compiler allocates a base address and sufficient storage to contain all elements.
Base address is the address of the first element of the array ( i.e. &x[0]).
In addition, the array name x is defined internally as constant pointer(const int *) to first
element.