In C programming, a pointer is a variable that stores the memory address of another variable. Pointers are powerful and flexible features in C, allowing direct manipulation of memory and providing a way to implement dynamic memory allocation. Here are some key concepts related to pointers:
- Declaration and Initialization:
- To declare a pointer, you use the
*
(asterisk) symbol. For example,int *ptr;
declares a pointer to an integer. - Pointers should be initialized before use. You can initialize a pointer by assigning it the address of a variable of the appropriate type. For example:cCopy code
int x = 10; int *ptr = &x; // ptr now points to the address of x
- To declare a pointer, you use the
- Accessing Values through Pointers:
- To access the value stored at the memory location pointed to by a pointer, you use the
*
operator. For example:cCopy codeint y = *ptr; // y now contains the value of x (10)
- To access the value stored at the memory location pointed to by a pointer, you use the
- Pointer Arithmetic:
- Pointers can be manipulated using arithmetic operations. Incrementing a pointer moves it to the next memory location of its type. For example:cCopy code
int arr[5] = {1, 2, 3, 4, 5}; int *p = arr; // points to the first element of the array int thirdElement = *(p + 2); // accesses the third element using pointer arithmetic
- Pointers can be manipulated using arithmetic operations. Incrementing a pointer moves it to the next memory location of its type. For example:cCopy code
- Dynamic Memory Allocation:
- Pointers are commonly used with functions like
malloc
,calloc
, andrealloc
to allocate memory dynamically. This allows you to manage memory at runtime.cCopy codeint *dynamicArray = (int*)malloc(5 * sizeof(int));
- Pointers are commonly used with functions like
- Null Pointers:
- Pointers can be assigned a special value called
NULL
to indicate that they do not point to any valid memory address.cCopy codeint *nullPtr = NULL;
- Pointers can be assigned a special value called
- Pointer to Functions:
- Pointers can also point to functions, allowing for more advanced programming techniques such as callbacks.cCopy code
int add(int a, int b) { return a + b; } int (*ptrFunc)(int, int) = add; // pointer to a function int result = ptrFunc(3, 4); // calls the function through the pointer
- Pointers can also point to functions, allowing for more advanced programming techniques such as callbacks.cCopy code
- Pointers and Arrays:
- Arrays in C are closely related to pointers. In many contexts, the name of an array is treated as a pointer to its first element.
Pointers require careful use to avoid common pitfalls like segmentation faults and undefined behavior. Memory management and pointer arithmetic should be done with caution to ensure the stability and correctness of your programs.
Program :
#include<stdio.h>
#include<conio.h>
main()
{
int a;
int *pointer=&a;
printf(“Enter the value of a : “);
scanf(“%d”,&a);
printf(“\nThe value of a is : %d”,a);
printf(“\nThe address of a is : %x”,&a);
printf(“\nThe value of pointer is : %x”,pointer);
printf(“\nThe address of pointer is : %x”,&pointer);
getch();
}