- A pointer is a variable that stores the address of another variable. Pointers allow dynamic memory allocation and access to the value of the variable being pointed to using the indirection operator (*).
- Pointer variables are declared with a data type followed by an asterisk, such as int *ptr. They can be initialized by using the address-of operator (&) to store the address of another variable.
- Pointers can be used to access elements in an array by using pointer arithmetic. An array name itself is a constant pointer to the first element of the array.
2. Pointer
• A pointer is a variable that stores / points the address
of another variable.
• A pointer is used to allocate memory dynamically i.e.
at run time.
• The pointer variable may belong to any of the data
types such as int, float, char, double, etc.
• The size of a pointer depends on the architecture.
However, in 32-bit architecture, the size of a pointer
is 2 bytes.
Syntax :
• data_type *var_name;
3. • Example:
• int *p, where * is used to denote that p is a pointer
variable.
• int *a; // pointer to integer
• char *c; // pointer to character
• Suppose int n = 10;
• int * p = &n; Here p is a pointer
variable pointing to the address of the variable n of typ
e integer.
4. Memory cell Address
0
1
65535
• A computer’s memory is a sequential collection of storage cells , each cell is called a byte and
has a number called address associated with it
• The address starts with 0 and the last address depends on the memory size
• A computer system having 64 k memory will have its last address as 65535
5. Example:
#include<stdio.h>
int main(){
int number=100;
int *p;
p=&number; //stores the address of variable number
printf“(Address of p is %x n", p); // p contains the
address of the number
printf("Value of p is %d n",*p); // *p contains the
value stored at the address contained by p.
return 0;
}
7. Initialization of pointer
• The process of assigning the address of a variable to a
pointer variable is called Initialization
• Once a pointer variable has been declared, we can use
the assignment operator to initialize the variable
Eg:
int quantity;
int *p; // declaration
p=&quantity; // Initialization
// declaration and initialization
int *p =&quantity
9. Accessing a variable through its pointer
• Once a pointer has been assigned the address of a
variable, accessing the value of the variable is done
with the help of indirection operator(*)
int quantity;
quantity=179;
p=&quantity;
n=*p
*p returns the value of the variable of which the pointer
value is the address
*p returns the value of the variable quantity, as p is the
address of quantity
10. #include<stdio.h>
void main()
{
int x,y;
int *ptr;
x=10;
ptr=&x;
y=*ptr;
printf("value of x is %dn",x);
printf("Address of x is %un",&x);
printf("Address of x is %un",ptr);
printf("value of x is %dn",y);
}
11. Output:
value of x is 10
Address of x is 2514795568
Address of x is 2514795568
value of x is 10
12. Pointer and Array
• When an array is declared, the compiler allocates
sufficient amount of memory to contain all the
elements of the array. Base address is the address of
the first element of an array.
Consider, int arr[5] = { 1, 2, 3, 4, 5 };
• Assuming that the base address of arr is 2000 and each
integer requires two bytes, the five elements will be
stored as follows:
13. Element arr[0] arr[1] arr[2] arr[3] arr[4]
Address 2000 2002 2004 2006 2008
• Here variable arr gives the base address, which is a
constant pointer pointing to the first element of the array.
• Hence arr contains the address of arr[0] i.e 2000. Therefore, arr
is equal to &arr[0].
• Now we can access every element of the array arr by
incrementing the pointer to move from one element to
another.
1 2 3 4 5
14. Pointer to Array
We can use a pointer to point to an array and then we can use that pointer to access the array
elements.
Example:
#include <stdio.h>
int main()
{
int i;
int a[5] = {1, 2, 3, 4, 5};
int *p = a; // Similar to int*p = &a[0]
for (i = 0; i < 5; i++)
{
printf("%dn", *p);
p++;
}
return 0;
}
16. Array of Pointers
• Like an array of int,float, etc, we can also declare an array of
pointers.
Syntax: datatype *array_name[size];
Example:
int *arr_ptr[5];
Here arr_ptr is an array of 5 integer pointers. It means that this
array can hold the address of 5 integer variables.
17. Example:
#include<stdio.h>
int main()
{
int *arr_ptr[3];
int a = 10, b = 20, c = 50, i;
arr_ptr[0] = &a;
arr_ptr[1] = &b;
arr_ptr[2] = &c;
for(i = 0; i < 3; i++)
{
printf("Address = %dt Value = %dn", arr_ptr[i], *arr_ptr[i]);
}
• return 0;
• }
19. Pointer to a function
A function pointer is a pointer that refers to the address
of a function.
Syntax:
type (*pointer-name)(parameter);
Example:
1. int (*sum)();
2. int sum(int, int);
int (*s)(int, int);
s = sum;
• Here s is a pointer to a function sum. Now sum can be called
using the function pointer s(10, 20);
20. Example:
#include <stdio.h>
int add(int x, int y)
{
return x+y;
}
void main( )
{
int (*fp)(int, int);
fp = add;
int s = fp(50, 25);
printf("Sum is %d", s);
}
22. Pointer to a Pointer
• A pointer is used to store the address of a variable.
• If one pointer stores the address of another pointer, it is known
as Pointer to Pointer or Double Pointer.
• The first pointer is used to store the address of a variable
whereas the second pointer is used to store the address of the
first pointer.
23. Syntax:
datatype **pointer-variable;
#include<stdio.h>
void main ()
{
int a = 10;
int *p;
int **pp;
p = &a;
pp = &p;
printf("address of a: %xn",p);
printf("address of p: %xn",pp);
printf("value stored at p: %dn",*p);
printf("value stored at pp: %dn",**pp);
}
24. Output:
address of a: 364eb8ac
address of p: 364eb8b0
value stored at p: 10
value stored at pp: 10
25. Pointer Expressions
• Like variables, pointer variables can be used in expressions
Eg: let p1 and p2 are pointers, declared and initialized
Then
y= *p1 * *p2; // valid; same as (*p1) * (*p2)
sum = sum + *p1;
z= 5* - *p2/ *p1; // (5*( -(*p2))) / (*p1)
*p2=*p2+10;
26. Pointer Arithmetic
• C allows us to add integers to or subtract integers from
pointers
• One pointer can be subtracted from another pointer
Eg:
p1+4,
p2-2
p1-p2
• The increment operator can also be applied to an operand of
pointer type
29. Pointer Arithmetic
• Pointers cant be used in division or multiplication
Eg:
p1 / p2, p1* p2 or p1/3 are not allowed
• Two pointers cant be added
Eg:
p1+p2 is not legal
33. Pointers and strings
• We know that strings are treated as character arrays
• They can be initialized as follows:
• char str[5] =“good”;
• The compiler will automatically inserts null character ‘0’ at the
end of the string
• C provides a method to create strings using pointer variables of
type char
• char *str=“good”;
• This will create a string and stores its address in the pointer
variable str and str points to the 1st character of the string
“good”
37. Null Pointer
• It is a pointer which doesn’t point to any value
• To declare a null pointer, the predefined constant NULL is used.
NULL is defined in several headers files including <stdio.h>,
<string.h>, etc
• We can also a pointer as NULL pointer by using a constant 0
Eg:
int *ptr=NULL;
or
int *ptr1=0;