C Pointers
C Pointers
C Pointers
A pointer is defined as a derived data type that can store the address of
other C variables or a memory location. We can access and manipulate the
data stored in that memory location using pointers.
As the pointers in C store the memory addresses, their size is
independent of the type of data they are pointing to. This size of
pointers in C only depends on the system architecture.
2
Syntax of C Pointers
datatype * ptr;
where
•ptr is the name of the pointer.
•datatype is the type of data it is pointing to.
The above syntax is used to define a pointer to a variable. We can also
define pointers to functions, structures, etc.
3
How to Use Pointers?
1. Pointer Declaration
In pointer declaration, we only declare the pointer but do not initialize it. To declare a
pointer, we use the ( * ) dereference operator before its name.
Example
int *ptr;The pointer declared here will point to some random memory address as it is not
initialized. Such pointers are called wild pointers.
How to Use Pointers?
2. Pointer Initialization
Pointer initialization is the process where we assign some initial value to the pointer variable. We
generally use the ( & ) addressof operator to get the memory address of a variable and then store
it in the pointer variable.
Example
Example
3. Pointer Dereferencing
Dereferencing a pointer is the process of accessing the value stored in the memory address
specified in the pointer. We use the same ( * ) dereferencing operator that we used in the pointer
declaration.
Size of Pointers in C
The size of the pointers in C is equal for every pointer type. The size of the pointer does not
depend on the type it is pointing to. It only depends on the operating system and CPU
architecture. The size of pointers in C is
•8 bytes for a 64-bit System
•4 bytes for a 32-bit System
The reason for the same size is that the pointers store the memory addresses, no matter what type
they are. As the space required to store the addresses of the different memory locations is the
same, the memory required by one pointer type will be equal to the memory required by other
pointer types.
How to find the size of pointers in C?
We can find the size of pointers using the sizeof operator as shown in the following program:
C Program to find the size of different pointer types.
#include <stdio.h>
// dummy structure
struct str {
};
// dummy function
void func(int a, int b){};
int main()
{
// dummy variables definitions
int a = 10;
char c = 'G';
struct str x;
C Program to find the size of different pointer types.
// pointer definitions of different types
int* ptr_int = &a;
char* ptr_char = &c;
struct str* ptr_str = &x;
void (*ptr_func)(int, int) = &func;
void* ptr_vn = NULL;
// printing sizes
printf("Size of Integer Pointer \t:\t%d bytes\n",
sizeof(ptr_int));
printf("Size of Character Pointer\t:\t%d bytes\n",
sizeof(ptr_char));
printf("Size of Structure Pointer\t:\t%d bytes\n",
sizeof(ptr_str));
printf("Size of Function Pointer\t:\t%d bytes\n",
sizeof(ptr_func));
printf("Size of NULL Void Pointer\t:\t%d bytes",
sizeof(ptr_vn));
return 0;
C Program to find the size of different pointer types.
Output
Size of Integer Pointer : 8 bytes
Size of Character Pointer : 8 bytes
Size of Structure Pointer : 8 bytes
Size of Function Pointer : 8 bytes
Size of NULL Void Pointer : 8 bytes
C Pointer Arithmetic
The Pointer Arithmetic refers to the legal or valid arithmetic
operations that can be performed on a pointer
#include <stdio.h>
int main()
{
// Declare an array
int v[3] = { 10, 100, 200 };
Output
Value of *ptr = 10
Value of ptr = 0x7ffe8ba7ec50
In C programming language, pointers and arrays are closely related. An array name acts like a
pointer constant. The value of this pointer constant is the address of the first element. For
example, if we have an array named val then val and &val[0] can be used interchangeably.
If we assign this value to a non-constant pointer of the same type, then we can access the
elements of the array using this pointer.
Example 1: Accessing Array Elements using Pointer with Array Subscript
C Program to access array elements using pointer
#include <stdio.h>
void geeks()
{
// Declare an array
int val[3] = { 5, 10, 15 };
return;
}
// Driver program
int main()
{
C Program to access array elements using pointer
Output
Elements of the array are: 5 10 15
Uses of Pointers in C
The C pointer is a very powerful tool that is widely used in C
programming to perform various useful operations. It is used to achieve
the following functionalities in C:
1.Pass Arguments by Reference
2.Accessing Array Elements
3.Return Multiple Values from Function
4.Dynamic Memory Allocation
5.Implementing Data Structures
6.In System-Level Programming where memory addresses are useful.
7.In locating the exact value at some memory location.
8.To avoid compiler confusion for the same variable name.
9.To use in Control Tables.