C Pointers
The pointer in C language is a variable which stores the address of another variable.
This variable can be of type int, char, array, function, or any other pointer. The size of
the pointer depends on the architecture. However, in 32-bit architecture the size of a
pointer is 2 byte.
Consider the following example to define a pointer which stores the address of an
integer.
1. int n = 10;
2. int* p = &n; // Variable p of type pointer is pointing to the address of the variable n
of type integer.
Declaring a pointer
The pointer in c language can be declared using * (asterisk symbol). It is also known as
indirection pointer used to dereference a pointer.
1. int *a; //pointer to int
2. char *c; //pointer to char
1. #include<stdio.h>
2. int main(){
3. int number=50;
4. int *p;
5. p=&number;//stores the address of number variable
6. printf("Address of p variable is %x \n",p); // p contains the address of the number the
before printing p gives the address of number.
7. printf("Value of p variable is %d \n",*p); // As we know that * is used to dereference a
pointer therefore if we print *p, we will get the value stored at the address contained
by p.
8. return 0;
9. }
Output
Address of number variable is fff4
Address of p variable is fff4
Value of p variable is 50
Advantage of pointer
1) Pointer reduces the code and improves the performance, it is used to retrieving
strings, trees, etc. and used with arrays, structures, and functions.
2) We can return multiple values from a function using the pointer.
3) It makes you able to access any memory location in the computer's memory.
Usage of pointer
There are many applications of pointers in c language.
1) Dynamic memory allocation
In c language, we can dynamically allocate memory using malloc() and calloc()
functions where the pointer is used.
2) Arrays, Functions, and Structures
Pointers in c language are widely used in arrays, functions, and structures. It reduces
the code and improves the performance.
Address Of (&) Operator
The address of operator '&' returns the address of a variable. But, we need to use %u
to display the address of a variable.
1. #include<stdio.h>
2. int main(){
3. int number=50;
4. printf("value of number is %d, address of number is %u",number,&number);
5. return 0;
6. }
Output
value of number is 50, address of number is fff4
Pointer Program to swap two numbers without using
the 3rd variable.
1. #include<stdio.h>
2. int main(){
3. int a=10,b=20,*p1=&a,*p2=&b;
4. printf("Before swap: *p1=%d *p2=%d",*p1,*p2);
5. *p1=*p1+*p2;
6. *p2=*p1-*p2;
7. *p1=*p1-*p2;
8. printf("\nAfter swap: *p1=%d *p2=%d",*p1,*p2);
9. return 0;
10. }
Output
Before swap: *p1=10 *p2=20
After swap: *p1=20 *p2=10
Arithmetic Pointer
A pointer in c is an address, which is a numeric value. Therefore, you can perform
arithmetic operations on a pointer just as you can on a numeric value. There are four
arithmetic operators that can be used on pointers: ++, --, +, and -
To understand pointer arithmetic, let us consider that ptr is an integer pointer which
points to the address 1000. Assuming 32-bit integers, let us perform the following
arithmetic operation on the pointer −
ptr++
After the above operation, the ptr will point to the location 1004 because each time
ptr is incremented, it will point to the next integer location which is 4 bytes next to the
current location.
#include <stdio.h>
const int MAX = 3;
int main () {
int var[] = {10, 100, 200};
int i, *ptr;
/* let us have array address in pointer */
ptr = var;
for ( i = 0; i < MAX; i++) {
printf("Address of var[%d] = %x\n", i, ptr );
printf("Value of var[%d] = %d\n", i, *ptr );
/* move to the next location */
ptr++;
}
return 0;
}
When the above code is compiled and executed, it produces the following result −
Address of var[0] = bf882b30
Value of var[0] = 10
Address of var[1] = bf882b34
Value of var[1] = 100
Address of var[2] = bf882b38
Value of var[2] = 200
Arrays and Pointers
#include <stdio.h>
const int MAX = 3;
int main () {
int var[] = {10, 100, 200};
int i, *ptr[MAX];
for ( i = 0; i < MAX; i++) {
ptr[i] = &var[i]; /* assign the address of integer. */
}
for ( i = 0; i < MAX; i++) {
printf("Value of var[%d] = %d\n", i, *ptr[i] );
}
return 0;
}
When the above code is compiled and executed, it produces the following result −
Value of var[0] = 10
Value of var[1] = 100
Value of var[2] = 200
C Function Pointer
As we know that we can create a pointer of any data type such as int, char, float, we
can also create a pointer pointing to a function. The code of a function always resides
in memory, which means that the function has some address. We can get the address
of memory by using the function pointer.
Let's see a simple example.
#include <stdio.h>
1. int main()
2. {
3. printf("Address of main() function is %p",main);
4. return 0;
5. }
The above code prints the address of main() function.
Output
Declaration of a function pointer
Till now, we have seen that the functions have addresses, so we can create pointers
that can contain these addresses, and hence can point them.
Syntax of function pointer
1. return type (*ptr_name)(type1, type2…);
For example:
1. int (*ip) (int);
In the above declaration, *ip is a pointer that points to a function which returns an int
value and accepts an integer value as an argument.
1. float (*fp) (float);
In the above declaration, *fp is a pointer that points to a function that returns a float
value and accepts a float value as an argument.
We can observe that the declaration of a function is similar to the declaration of a
function pointer except that the pointer is preceded by a '*'. So, in the above
declaration, fp is declared as a function rather than a pointer.
1. float (*fp) (int , int); // Declaration of a function pointer.
2. float func( int , int ); // Declaration of function.
3. fp = func; // Assigning address of func to the fp pointer.
In the above declaration, 'fp' pointer contains the address of the 'func' function.
Calling a function through a function pointer
We already know how to call a function in the usual way. Now, we will see how to call
a function using a function pointer.
Suppose we declare a function as given below:
1. float func(int , int); // Declaration of a function.
Calling an above function using a usual way is given below:
1. result = func(a , b); // Calling a function using usual ways.
Calling a function using a function pointer is given below:
1. result = (*fp)( a , b); // Calling a function using function pointer.
Or
1. result = fp(a , b); // Calling a function using function pointer, and indirection
operator can be removed.
The effect of calling a function by its name or function pointer is the same. If we are
using the function pointer, we can omit the indirection operator as we did in the
second case. Still, we use the indirection operator as it makes it clear to the user that
we are using a function pointer.
Let's understand the function pointer through an example.
1. #include <stdio.h>
2. int add(int,int);
3. int main()
4. {
5. int a,b;
6. int (*ip)(int,int);
7. int result;
8. printf("Enter the values of a and b : ");
9. scanf("%d %d",&a,&b);
10. ip=add;
11. result=(*ip)(a,b);
12. printf("Value after addition is : %d",result);
13. return 0;
14. }
15. int add(int a,int b)
16. {
17. int c=a+b;
18. return c;
19. }
Output
1. malloc()
2. calloc()
3. free()
4. realloc()
C malloc() method
The “malloc” or “memory allocation” method in C is used to dynamically
allocate a single large block of memory with the specified size.
Syntax:
ptr = (cast-type*) malloc(byte-size)
For Example:
ptr = (int*) malloc(100 * sizeof(int));
Since the size of int is 4 bytes, this statement will allocate 400 bytes of mem
ory. And, the pointer ptr holds the address of the first byte in the allocated m
emory.
C calloc() method
1. “calloc” or “contiguous allocation” method in C is used to dynamically
allocate the specified number of blocks of memory of the specified type.
2. Syntax:
3. ptr = (cast-type*)calloc(n, element-size);
4. here, n is the no. of elements and element-size is the size of
each element.
5. For Example:
6. ptr = (float*) calloc(25, sizeof(float));
This statement allocates contiguous space in memory for 25 elements
each with the size of the float.
7.
C free() method
“free” method in C is used to dynamically de-allocate the memory. The
memory allocated using functions malloc() and calloc() is not de-allocated on
their own. Hence the free() method is used
Syntax:
free(ptr);
C realloc() method
“realloc” or “re-allocation” method in C is used to dynamically change the
memory allocation of a previously allocated memory. In other words, if the
memory previously allocated with the help of malloc or calloc is insufficient,
realloc can be used to dynamically re-allocate memory.
Syntax:
ptr = realloc(ptr, newSize);
where ptr is reallocated with new size 'newSize'.