Pointers in C
Pointers in C
Pointers in C
What is a Pointer in C?
C pointer is the derived data type that is used to store the address of another
variable and can also be used to access and manipulate the variable's data stored at
that location. The pointers are considered as derived data types.
With pointers, you can access and modify the data located in the memory, pass the
data efficiently between the functions, and create dynamic data structures like linked
lists, trees, and graphs.
Pointer Declaration
To declare a pointer, use the dereferencing operator (*) followed by the data
type.
Syntax
type *var-name;
Here, type is the pointer's base type; it must be a valid C data type and var-name
is the name of the pointer variable. The asterisk * used to declare a pointer is the
same asterisk used for multiplication. However, in this statement the asterisk is
being used to designate a variable as a pointer.
The actual data type of the value of all pointers, whether integer, float, character, or
otherwise, is the same, a long hexadecimal number that represents a memory
https://www.tutorialspoint.com/cprogramming/c_pointers.htm 1/14
6/16/24, 12:50 PM Pointers in C
address. The only difference between pointers of different data types is the data type
of the variable or constant that the pointer points to.
Pointer Initialization
After declaring a pointer variable, you need to initialize it with the address of another
variable using the address of (&) operator. This process is known as referencing
a pointer.
Syntax
pointer_variable = &variable;
Example
int x = 10;
int *ptr = &x;
Here, x is an integer variable, ptr is an integer pointer. The pointer ptr is being
initialized with x.
https://www.tutorialspoint.com/cprogramming/c_pointers.htm 2/14
6/16/24, 12:50 PM Pointers in C
Example
In the below example, we are taking an integer variable with its initial value and
changing it with the new value.
#include <stdio.h>
int main() {
int x = 10;
return 0;
}
Output
Value of x = 10
Value of x = 20
https://www.tutorialspoint.com/cprogramming/c_pointers.htm 3/14
6/16/24, 12:50 PM Pointers in C
You can use pointers with any type of variable such as integer, float, string, etc. You
can also use pointers with derived data types such as array, structure, union, etc.
Example
In the below example, we are using pointers for getting values of different types of
variables.
#include <stdio.h>
int main() {
int x = 10;
float y = 1.3f;
char z = 'p';
return 0;
}
Output
Value of x = 10
Value of y = 1.300000
Value of z = p
https://www.tutorialspoint.com/cprogramming/c_pointers.htm 4/14
6/16/24, 12:50 PM Pointers in C
Example
In the below example, we are printing the size of different types of pointers:
#include <stdio.h>
int main() {
int x = 10;
float y = 1.3f;
char z = 'p';
return 0;
}
Output
Examples of C Pointers
Practice the following examples to learn the concept of pointers –
https://www.tutorialspoint.com/cprogramming/c_pointers.htm 5/14
6/16/24, 12:50 PM Pointers in C
The following example shows how you can use the & and * operators to carry out
pointer-related opeartions in C −
#include <stdio.h>
int main(){
return 0;
}
Output
We will declare an int variable and display its value and address −
#include <stdio.h>
int main(){
https://www.tutorialspoint.com/cprogramming/c_pointers.htm 6/14
6/16/24, 12:50 PM Pointers in C
return 0;
}
Output
In this example, the address of var is stored in the intptr variable with & operator
#include <stdio.h>
int main(){
return 0;
}
Output
Variable: 100
Address of Variable: 0x7ffdcc25860c
intptr: 0x7ffdcc25860c
Address of intptr: 0x7ffdcc258610
https://www.tutorialspoint.com/cprogramming/c_pointers.htm 7/14
6/16/24, 12:50 PM Pointers in C
Example 5
Now let's take an example of a float variable and find its address −
#include <stdio.h>
int main(){
Output
var1: 10.550000
Address of var1: 1512452612
We can see that the address of this variable (any type of variable for that matter) is
an integer. So, if we try to store it in a pointer variable of "float" type, see what
happens −
The compiler doesn’t accept this, and reports the following error −
Note: The type of a variable and the type of its pointer must be same.
In C, variables have specific data types that define their size and how they store
values. Declaring a pointer with a matching type (e.g., float *) enforces "type
compatibility" between the pointer and the data it points to.
Different data types occupy different amounts of memory space in C. For example,
an "int" typically takes 4 bytes, while a "float" might take 4 or 8 bytes depending on
https://www.tutorialspoint.com/cprogramming/c_pointers.htm 8/14
6/16/24, 12:50 PM Pointers in C
the system. Adding or subtracting integers from pointers moves them in memory
based on the size of the data they point to.
#include <stdio.h>
int main(){
return 0;
}
Output
var1: 10.550000
Address of var1: 0x7ffc6daeb46c
floatptr: 0x7ffc6daeb46c
Address of floatptr: 0x7ffc6daeb470
Pointer to Pointer
We may have a pointer variable that stores the address of another pointer itself.
https://www.tutorialspoint.com/cprogramming/c_pointers.htm 9/14
6/16/24, 12:50 PM Pointers in C
In the above figure, "a" is a normal "int" variable, whose pointer is "x". In turn, the
variable stores the address of "x".
Note that "y" is declared as "int **" to indicate that it is a pointer to another pointer
variable. Obviously, "y" will return the address of "x" and "*y" is the value in "x"
(which is the address of "a").
To obtain the value of "a" from "y", we need to use the expression "**y". Usually, "y"
will be called as the pointer to a pointer.
Example
#include <stdio.h>
int main(){
return 0;
}
https://www.tutorialspoint.com/cprogramming/c_pointers.htm 10/14
6/16/24, 12:50 PM Pointers in C
Output
var: 10
Address of var: 951734452
inttptr: 951734452
Address of inttptr: 951734456
var: 10
Value at intptr: 10
ptrptr: 951734456
Address of ptrtptr: 951734464
intptr: 951734452
Value at ptrptr: 951734452
var: 10
*intptr: 10
**ptrptr: 10
You can have a pointer to an array as well as a derived type defined with struct.
Pointers have important applications. They are used while calling a function by
passing the reference. Pointers also help in overcoming the limitation of a function’s
ability to return only a single value. With pointers, you can get the effect of returning
multiple values or arrays.
NULL Pointers
It is always a good practice to assign a NULL value to a pointer variable in case you
do not have an exact address to be assigned. This is done at the time of variable
declaration. A pointer that is assigned NULL is called a null pointer.
The NULL pointer is a constant with a value of "0" defined in several standard
libraries.
Example
https://www.tutorialspoint.com/cprogramming/c_pointers.htm 11/14
6/16/24, 12:50 PM Pointers in C
#include <stdio.h>
int main(){
return 0;
}
Output
When the above code is compiled and executed, it produces the following result −
The memory address "0" has a special significance; it signals that the pointer is not
intended to point to an accessible memory location. But by convention, if a pointer
contains the null (zero) value, it is assumed to point to nothing.
Example
Consider the following example, which prints the address of the variables defined −
#include <stdio.h>
https://www.tutorialspoint.com/cprogramming/c_pointers.htm 12/14
6/16/24, 12:50 PM Pointers in C
int main(){
int var1;
char var2[10];
return 0;
}
Output
When the above code is compiled and executed, it will print the address of the
variables −
Pointers in Detail
Pointers have many but easy concepts and they are very important to C
programming. The following important pointer concepts should be clear to any C
programmer −
Pointer arithmetic
1 There are four arithmetic operators that can be used in pointers: ++, --,
+, -
Array of pointers
2
You can define arrays to hold a number of pointers.
Pointer to pointer
3
C allows you to have pointer on a pointer and so on.
https://www.tutorialspoint.com/cprogramming/c_pointers.htm 13/14
6/16/24, 12:50 PM Pointers in C
https://www.tutorialspoint.com/cprogramming/c_pointers.htm 14/14