Pointers
Pointers
Pointer Arithmetic, Using Pointers with Arrays, Using Pointers with Strings,
Array of Pointers, Pointers as function arguments, Functions returning
pointers.
Point to note: %p is a format specifier which is used for displaying the address in hex
format.
Example:
int *p1 /*Pointer to an integer variable*/
double *p2 /*Pointer to a variable of data type double*/
char *p3 /*Pointer to a character variable*/
float *p4 /*pointer to a float variable*/
The above are the few examples of pointer declarations. If you need a pointer to store the
address of integer variable then the data type of the pointer should be int. Same case is with
the other data types.
For example:
int var = 10;
int *ptr;
p = &var;
Reference operator(&):
Address of operator (“&”) is known as referencing operator.
This operator returns the address of the variable associated with the operator.
For e.g., if we write “&x”, it will return the address of the variable “x’.
Hence, if we have a pointer “p”, which we want to point to a variable x, then we need to
copy the address of the variable “x” in the pointer variable “p”.
This is implemented by the statement: p = &x;
Dereference operator(*):
Value of operator (“*”) is known as dereference operator.
This operator returns the value stored in the variable pointed by the specified pointer.
For e.g., if we write “*p”, it will return the value of the variable pointed by the pointer “p”.
Hence, if we want the value of the variable pointed by the pointer “p” to be stored in a
variable “y”, then the expression can be written as: y = *p;
Pointer Arithmetic
A pointer in c is an address, which is a numeric value. Therefore, you can perform
arithmetic operationson 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 address1000. Assuming 16-bit integers, let us perform the following
arithmetic operation on the pointer
ptr++
After the above operation, the ptr will point to the location 1002 because each time ptr
is incremented, itwill point to the next integer location which is 2 bytes next to the
current location. This operation will move the pointer to the next memory location
without impacting the actual value at the memory location.If ptr points to a character
whose address is 1000, then the above operation will point to the location 1001 because
the next character will be available at 1001.
#include<stdio.h>
int main()
{
int arr[5] = { 10, 20, 30, 40, 50 };
int *ptr = arr;
printf("%p\n", ptr);return 0;
}
In this program, we have a pointer ptr that points to the 0th element of the array
As studied above, we can use a pointer to point to an array, and then we can use that
pointer to access thearray elements
OUTPUT:
10 20 30 40 50
In the above program, the pointer *ptr will print all the values stored in the array one by
one. We can alsouse the Base address (a in above case) to act as a pointer and print all
the values. Base address is an address of a first value stored in an array.
Pointers as Function Argument in C (Pass by Reference):
Pointer as a function parameter is used to hold addresses of arguments passed during
functioncall. This is also known as call by reference. When a function is called by
reference any change made to the reference variable will affect the original variable.
printf("m = %d\n",
m);printf("n
= %d\n\n", n);
/*
pointer 'a' and 'b' holds and
points to the address of 'm'
and 'n'
*/
void swap(int *a, int *b)
{
int
temp;
temp
= *a;
*a = *b;
*b = temp;
}
Output:
Pointer to functions
A pointer to a function points to the address of the executable code (Function
Definition) ofthe function. Following is a simple example that shows declaration and
function call using function pointer.,
Syntax:
data_type (*pointer-name)(parameter);
Here is an example :
int (*sum)(); //legal declaration of pointer to function
int *sum(); //This is not a declaration of pointer to function
A function pointer can point to a specific function when it is assigned the name of that
Here is a pointer to a function sum. Now sum can be called using function pointer s along
withproviding the required argument values.
s (10, 20);
return 0;
}
Output:
25
Function returning Pointer:
Pointers in C programming language is a variable which is used to store the memory address
of another variable. We can pass pointers to the function as well as return pointer from a
function. But it is not recommended to return the address of a local variable outside the
function as it goes out of scope after function returns.
Program 1:
The below program will give segmentation fault since ‘A’ was local to the function
#include <stdio.h>
// Driver Code
int main()
{
// Declare a pointer
int* p;
// Function call
p = fun();
printf("%p\n", p);
printf("%d\n", *p);
return 0;
}
Output:
ERROR
Explanation:
The main reason behind this scenario is that compiler always make a stack for a
function call. As soon as the function exits the function stack also gets removed
which causes the local variables of functions goes out of scope.
Static Variables have a property of preserving their value even after they are out of
their scope. So to execute the concept of returning a pointer from function in C you
must define the local variable as a static variable.
Program 2:
// C program to illustrate the concept of
// returning pointer from a function
#include <stdio.h>
// Driver Code
int main()
{
// Declare a pointer
int* p;
// Function call
p = fun();
// Print Address
printf("%p\n", p);
Output:
0x601038
10