What is a Pointer in C? Understanding Pointers and Their Uses
Learn about pointers in C programming, a powerful tool for storing memory addresses and enabling efficient data manipulation. Explore how pointers allow direct access to memory, facilitate dynamic data structures, and optimize function data passing. Understand pointer declaration and its key role in C.
What is a Pointer in C?
A pointer in C is a derived data type used to store the address of another variable. Pointers allow for accessing and modifying the data at that memory location, passing data efficiently between functions, and creating 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 and var-name
is the name of the pointer variable.
Pointer Initialization
After declaring a pointer variable, initialize it with the address of another variable using the address of (&) operator.
Example
int x = 10;
int *ptr = &x;
Referencing and Dereferencing Pointers
In C, you use the &
operator to get the address of a variable and the *
operator to get the value at that address.
Access and Manipulate Values using Pointers
To access or modify the value of a variable pointed to by a pointer, use the *
operator.
Example
#include <stdio.h>
int main() {
int x = 10;
int *ptr = &x;
// Printing the current value
printf("Value of x = %d\n", *ptr);
// Changing the value
*ptr = 20;
// Printing the updated value
printf("Value of x = %d\n", *ptr);
return 0;
}
Output
Value of x = 10
Value of x = 20
How to Use Pointers?
To use pointers, declare a pointer variable, initialize it with the address of another variable, and then use it to access and change the value of the variables.
Example
#include <stdio.h>
int main() {
int x = 10;
float y = 1.3f;
char z = 'p';
int *ptr_x = &x;
float *ptr_y = &y;
char *ptr_z = &z;
printf("Value of x = %d\n", *ptr_x);
printf("Value of y = %f\n", *ptr_y);
printf("Value of z = %c\n", *ptr_z);
return 0;
}
Output
Value of x = 10
Value of y = 1.300000
Value of z = p
Size of a Pointer Variable
The size of a pointer variable depends on the system architecture and does not depend on the type of data it points to.
Example
#include <stdio.h>
int main() {
int x = 10;
float y = 1.3f;
char z = 'p';
int *ptr_x = &x;
float *ptr_y = &y;
char *ptr_z = &z;
printf("Size of integer pointer : %lu\n", sizeof(ptr_x));
printf("Size of float pointer : %lu\n", sizeof(ptr_y));
printf("Size of char pointer : %lu\n", sizeof(ptr_z));
return 0;
}
Output
Size of integer pointer : 8
Size of float pointer : 8
Size of char pointer : 8
Examples of C Pointers
Example 1: Using Pointers in C
Example
#include <stdio.h>
int main(){
int var = 20;
int *ip;
ip = &var;
printf("Address of var variable: %p\n", &var);
printf("Address stored in ip variable: %p\n", ip);
printf("Value of *ip variable: %d\n", *ip);
return 0;
}
Output
Address of var variable: 0x7ffea76fc63c
Address stored in ip variable: 0x7ffea76fc63c
Value of *ip variable: 20
Example 2: Print Value and Address of an Integer
Example
#include <stdio.h>
int main(){
int var = 100;
printf("Variable: %d \t Address: %p", var, &var);
return 0;
}
Output
Variable: 100 Address: 0x7ffc62a7b844
Example 3: Integer Pointer
Example
#include <stdio.h>
int main(){
int var = 100;
int *intptr = &var;
printf("Variable: %d \nAddress of Variable: %p \n\n", var, &var);
printf("intptr: %p \nAddress of intptr: %p \n\n", intptr, &intptr);
return 0;
}
Output
Variable: 100
Address of Variable: 0x7ffdcc25860c
intptr: 0x7ffdcc25860c
Address of intptr: 0x7ffdcc258610
Example 4: Float Pointer
Example
#include <stdio.h>
int main(){
float var1 = 10.55;
float *floatptr = &var1;
printf("var1: %f \nAddress of var1: %p \n\n", var1, &var1);
printf("floatptr: %p \nAddress of floatptr: %p \n\n", floatptr, &floatptr);
printf("var1: %f \nValue at floatptr: %f", var1, *floatptr);
return 0;
}
Output
var1: 10.550000
Address of var1: 0x7ffc6daeb46c
floatptr: 0x7ffc6daeb46c
Address of floatptr: 0x7ffc6daeb470
var1: 10.550000
Value at floatptr: 10.550000
Example 5: Pointer to Pointer
Example
#include <stdio.h>
int main(){
int var = 10;
int *intptr = &var;
int **ptrptr = &intptr;
printf("var: %d \nAddress of var: %p \n\n", var, &var);
printf("intptr: %p \nAddress of intptr: %p \n\n", intptr, &intptr);
printf("Value at intptr: %d \n\n", *intptr);
printf("ptrptr: %p \nAddress of ptrptr: %p \n\n", ptrptr, &ptrptr);
printf("Value at ptrptr: %p \n\n", *ptrptr);
printf("var: %d \n*intptr: %d \n**ptrptr: %d", var, *intptr, **ptrptr);
return 0;
}
Output
var: 10
Address of var: 0x7ffdcc25860c
intptr: 0x7ffdcc25860c
Address of intptr: 0x7ffdcc258610
Value at intptr: 10
ptrptr: 0x7ffdcc258610
Address of ptrptr: 0x7ffdcc258620
Value at ptrptr: 0x7ffdcc25860c
var: 10
*intptr: 10
**ptrptr: 10
NULL Pointers
It is a good practice to assign NULL to a pointer variable when it does not point to any valid address. This prevents the pointer from pointing to an invalid memory location.
Example
#include <stdio.h>
int main(){
int *ptr = NULL;
printf("The value of ptr is : %x\n", ptr);
return 0;
}
Output
The value of ptr is 0
Address of the Variables
Every variable has a memory address that can be accessed using the ampersand (&) operator.
Example
#include <stdio.h>
int main(){
int var1;
char var2[10];
printf("Address of var1 variable: %x\n", &var1);
printf("Address of var2 variable: %x\n", &var2);
return 0;
}
Output
Address of var1 variable: 0x61e11508
Address of var2 variable: 0x61e1150e
Pointers in Detail
Pointers are crucial in C programming. Important concepts include:
- Pointer Arithmetic: Operators ++, --, +, - can be used with pointers.
- Array of Pointers: Arrays can hold multiple pointers.
- Pointer to Pointer: A pointer that stores the address of another pointer.
- Passing Pointers to Functions: Allows functions to modify variables in the calling function.
- Returning Pointers from Functions: Functions can return pointers to local variables, static variables, and dynamically allocated memory.