A pointer is a variable that stores the memory address of another variable. Pointers are one of the most important features of C because they allow programs to work directly with memory, arrays, functions, structures, and dynamic memory.
& operator obtains the address of
a variable, while the * operator is used to declare a pointer
and to access the value stored at the address held by a pointer.
A pointer is a variable that stores the memory address of another variable.
int age = 20;
int *ptr = &age;
Here, ptr stores the address of age.
Pointers are useful for:
Every variable is stored at a location in memory. The &
operator can be used to obtain its address.
int number = 50;
printf("%p", (void *)&number);
The %p format specifier is used to display a pointer value.
A pointer is declared using the * symbol.
int *ptr;
float *pricePtr;
char *charPtr;
The pointer type indicates the type of object that the pointer is intended to point to.
Use the address-of operator & to store the address of a
variable in a pointer.
int number = 100;
int *ptr = &number;
Now ptr points to number.
Dereferencing means accessing the value stored at the address held by a pointer.
The * operator is used for this purpose.
int number = 100;
int *ptr = &number;
printf("%d", *ptr);
The output is:
100
A pointer can be used to modify the value of the variable it points to.
int number = 10;
int *ptr = &number;
*ptr = 50;
printf("%d", number);
The value of number becomes 50.
#include <stdio.h>
int main()
{
int number = 25;
int *ptr = &number;
printf("Value = %d\n", number);
printf("Address = %p\n", (void *)&number);
printf("Pointer Value = %p\n", (void *)ptr);
printf("Value through Pointer = %d\n", *ptr);
return 0;
}
The pointer type should normally match the type of object it points to.
int number = 10;
int *ptr = &number;
float price = 25.5f;
float *pricePtr = &price;
char grade = 'A';
char *gradePtr = &grade;
A pointer contains an address, while dereferencing the pointer accesses the object stored at that address.
int x = 40;
int *p = &x;
printf("%p\n", (void *)p);
printf("%d\n", *p);
The first statement displays an address and the second displays the value.
A null pointer does not point to a valid object.
int *ptr = NULL;
Before dereferencing a pointer that may be null, check it first.
if(ptr != NULL)
{
printf("%d", *ptr);
}
The size of a pointer can be found using sizeof.
int *ptr;
printf("%zu", sizeof(ptr));
Pointer size depends on the target platform and implementation. Different pointer types may have different sizes on some systems.
Different pointers can point to different variables.
int a = 10;
int b = 20;
int *p1 = &a;
int *p2 = &b;
printf("%d\n", *p1);
printf("%d\n", *p2);
A pointer can be assigned the address stored in another compatible pointer.
int number = 50;
int *p1 = &number;
int *p2 = p1;
printf("%d", *p2);
Both pointers point to the same object.
Pointer arithmetic is especially useful when working with arrays.
int numbers[] = {10, 20, 30};
int *ptr = numbers;
printf("%d\n", *ptr);
ptr++;
printf("%d\n", *ptr);
After incrementing, ptr points to the next array element.
When a pointer to an array element is incremented, it advances to the next element of the pointed-to type.
int arr[] = {10, 20, 30};
int *p = arr;
p++;
printf("%d", *p);
The output is 20.
A pointer can also be decremented to move toward the previous element of an array.
int arr[] = {10, 20, 30};
int *p = &arr[2];
p--;
printf("%d", *p);
The output is 20.
The name of an array generally converts to a pointer to its first element when used in an expression.
int numbers[] = {10, 20, 30};
int *ptr = numbers;
printf("%d", *ptr);
The output is 10.
Array elements can be accessed using pointer arithmetic.
int numbers[] = {10, 20, 30, 40};
int *ptr = numbers;
printf("%d\n", *(ptr + 0));
printf("%d\n", *(ptr + 1));
printf("%d\n", *(ptr + 2));
printf("%d\n", *(ptr + 3));
The & and * operators have different purposes
in pointer operations.
int number = 100;
int *ptr = &number;
&number → address of numberptr → stores that address*ptr → value stored at that address
The scanf() function uses addresses to store input in variables.
int age;
printf("Enter age: ");
scanf("%d", &age);
Here, &age gives scanf() the address where the
input value should be stored.
A pointer can be passed to a function when the function needs to modify the original variable.
void change(int *p)
{
*p = 100;
}
int main()
{
int number = 10;
change(&number);
printf("%d", number);
return 0;
}
A function can receive multiple pointers.
void change(int *a, int *b)
{
*a = 50;
*b = 100;
}
int main()
{
int x = 10;
int y = 20;
change(&x, &y);
printf("%d %d", x, y);
return 0;
}
A pointer can also store the address of another pointer. This is called a pointer to pointer.
int number = 50;
int *p = &number;
int **pp = &p;
printf("%d", **pp);
Here, pp points to p, and **pp accesses
the value of number.
Pointers can point to characters as well.
char grade = 'A';
char *ptr = &grade;
printf("%c", *ptr);
The pointer type should match the type of the object it points to.
Do not dereference an uninitialized pointer.
int *ptr;
*ptr = 10;
The pointer does not point to a valid object here.
A pointer should first be assigned a valid address or set to
NULL before it is used appropriately.
Dereferencing a null pointer is invalid.
int *ptr = NULL;
printf("%d", *ptr);
Always ensure that a pointer points to a valid object before dereferencing it.
#include <stdio.h>
int main()
{
int number = 25;
int *ptr = &number;
printf("Value = %d\n", number);
printf("Address = %p\n", (void *)&number);
printf("Pointer = %p\n", (void *)ptr);
printf("Value using pointer = %d\n", *ptr);
*ptr = 75;
printf("New value = %d\n", number);
return 0;
}
This program demonstrates address access, pointer storage, dereferencing, and changing a variable through a pointer.
Pointers are used in many important areas of C programming.
Create a C program that:
Then create an array and use a pointer to display all of its elements.
& obtains the address of an object.* is used to declare a pointer and dereference it.NULL represents a null pointer value.%p with a (void *) conversion when displaying a pointer value with printf().Question: Which operator is used to access the value stored at the address held by a pointer?