0% found this document useful (0 votes)
8 views

bca class pointer

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

bca class pointer

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Introduction to Pointers:

A pointer in C or C++ is a variable that stores the memory address of


another variable. Pointers are powerful because they allow you to
manipulate memory directly, enable dynamic memory allocation, and
facilitate more efficient programming by allowing pass-by-reference in
function calls.

Characteristics of Pointers:
Memory Address: A pointer holds the address of a variable or object.
Dereferencing: The * operator is used to access or modify the value at the
memory location pointed to by the pointer.
Type-Specific: Pointers are type-specific, meaning a pointer to an int
cannot be used as a pointer to a float without casting.
Null Pointer: A pointer can be initialized to NULL or nullptr to indicate it
does not point to any valid memory.
Dynamic Allocation: Pointers enable the allocation of memory at runtime
using functions like malloc, calloc, new, and delete.
* and & Operators:
& (Address-of Operator):
Retrieves the address of a variable.
Example:
int a = 10;
int *p = &a; // p now stores the address of a
* (Dereference Operator):
Accesses the value at the memory location pointed to by the pointer.
Example:
int a = 10;
int *p = &a;
printf("%d", *p); // Output: 10
Pointer Type Declaration and Assignment:
Pointers are declared by placing * before the pointer name. The type
indicates what kind of data the pointer will point to.

Declaration:
int *p; // Pointer to an integer
char *c; // Pointer to a character
Assignment:
int a = 20;
int *p = &a; // p stores the address of a

Pointer Arithmetic
Pointer arithmetic allows you to navigate through memory. Operations
include addition, subtraction, and comparison.

Incrementing: Moves to the next memory location based on the size of the
data type.
int arr[] = {1, 2, 3};
int *p = arr;
p++; // Now points to the second element of the array
Decrementing: Moves to the previous memory location.
Difference: Finds the number of elements between two pointers.

int *p1 = &arr[0];


int *p2 = &arr[2];
printf("%d", p2 - p1); // Output: 2
Call by Reference:
Passing pointers to functions allows changes to be made directly to the
original variable.

void increment(int *num) {


(*num)++;
}

int main() {
int a = 5;
increment(&a);
printf("%d", a); // Output: 6
}
Passing Pointers to Functions:
Pointers can be passed to functions to save memory or manipulate arrays
or structures.

void display(int *arr, int size) {


for (int i = 0; i < size; i++) {
printf("%d ", *(arr + i));
}
}
Array of Pointers:
An array of pointers stores addresses of variables instead of values.

int a = 10, b = 20, c = 30;


int *arr[] = {&a, &b, &c};

printf("%d", *arr[1]); // Output: 20


Pointers to Functions:
Pointers can also store addresses of functions, allowing functions to be
passed as arguments.

int add(int a, int b) {


return a + b;
}

int main() {
int (*funcPtr)(int, int) = &add;
printf("%d", funcPtr(5, 10)); // Output: 15
}
Pointer to Pointer:
A pointer to a pointer stores the address of another pointer.

int a = 10;
int *p = &a;
int **pp = &p;
printf("%d", **pp); // Output: 10
Best Uses of Pointers:
Dynamic Memory Allocation: Allocate memory at runtime using malloc,
calloc, or new.
Efficient Array Handling: Use pointers to iterate through arrays efficiently.
Function Call Optimization: Pass large data structures by reference instead
of by value.
Implementation of Data Structures: Essential for linked lists, trees, and
graphs.
Hardware Interaction: Manipulate memory-mapped I/O registers directly.

You might also like