A pointer is a variable that stores a memory address, which can be used to access and manipulate the value stored at that memory location. Pointers allow for more efficient memory management and can be used to create dynamic data structures, such as linked lists and trees.
There are four fundamental things you need to know about pointers:
- How to declare them (with the address operator ‘
&
‘:int *pointer = &variable;
) - How to assign to them (
pointer = NULL;
) - How to reference the value to which the pointer points (known as dereferencing, by using the dereferencing operator ‘
*
‘:value = *pointer;
) - How they relate to arrays (the vast majority of arrays in C are simple lists, also called “1 dimensional arrays”).
Pointers can reference any data type, including basic types like integers and floating-point numbers, as well as more complex types like structures and classes. Pointers can also reference functions, which is known as a function pointer. Function pointers allow for more dynamic and flexible programming, such as creating callback functions or implementing function polymorphism.
Regarding relationship of pointers with text strings, In C, pointers can be used to manipulate strings stored in char arrays. Since a string is just an array of characters, we can manipulate a string by manipulating the memory it occupies using pointers.
Function pointers are similar in some ways to regular pointers, but they point to a memory location that contains a function. And they can be used to call a function indirectly through the pointer.
Here’s an example of using a pointer to manipulate a string in C:
1 2 3 4 5 6 7 8 | char str[] = "Hello, World!"; char *p = str; printf("%c\n", *p); // prints "H" p++; printf("%c\n", *p); // prints "e" |
In this example, we create a char array called “str” that contains the string “Hello, World!”. We then create a char pointer called “p” and initialize it with the address of the first character of the string. By using the * operator, we can access the value stored at the memory location pointed to by the pointer. By incrementing the pointer, we can move to the next character in the string.
Here’s an example of using a function pointer in C:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | #include <stdio.h> int add(int a, int b) { return a + b; } int subtract(int a, int b) { return a - b; } int main() { int (*p)(int, int); // Declare a function pointer p = add; // Assign the address of the add function to the pointer int result = p(3, 4); // Call the function through the pointer printf("%d\n", result); // prints "7" p = subtract; // Assign the address of the subtract function to the pointer result = p(3, 4); // Call the function through the pointer printf("%d\n", result); // prints "-1" } |
In this example, we have two functions, “add” and “subtract”, that take two integers and return an integer. We declare a function pointer called “p” that can be used to call any function that takes two integers and returns an integer. We then assign the address of the “add” function to the pointer and call the function through the pointer to get the result. We can also change the function pointer to point to the “subtract” function and call it in the same way.
Here’s an example of reversing an array in-place using pointers in C:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | #include <stdio.h> void reverse(int *arr, int size) { int *start = arr; int *end = arr + size - 1; while (start < end) { int temp = *start; *start = *end; *end = temp; start++; end--; } } int main() { int arr[] = {1, 2, 3, 4, 5}; int size = sizeof(arr) / sizeof(arr[0]); reverse(arr, size); for (int i = 0; i < size; i++) { printf("%d ", arr[i]); } return 0; } |
This program will output “5 4 3 2 1”.
The reverse() function takes a pointer to an integer array and the size of the array as arguments. It uses two pointers, one pointing to the first element of the array and the other pointing to the last element of the array. The function then swaps the elements at the two pointers and increments the first pointer and decrements the second pointer in each iteration until the two pointers meet in the middle. This in-place reversal of the array.
Keep in mind that the syntax and usage of pointers may vary depending on the programming language, but the concept remains the same.