What is Pointer in C
In C, a pointer is a variable that stores the memory address of another variable. Pointers are used to store the addresses of dynamically allocated memory, to pass arguments to functions by reference, and to work with arrays and strings.
A pointer is declared by placing an asterisk (*
) in front of the variable name. For example, here is how you might declare a pointer to an int
variable:
1 2 3 | int *ptr; |
The ptr
variable is a pointer to an int
. It does not hold the value of an int
, but rather holds the memory address of an int
variable.
To use a pointer, you must first assign it the address of a variable. You can do this using the &
operator, which returns the memory address of a variable. For example:
1 2 3 4 | int x = 10; int *ptr = &x; |
Here, the ptr
variable is assigned the address of the x
variable. You can then access the value of the x
variable through the pointer using the *
operator, also known as the “dereference” operator. For example:
1 2 3 | int y = *ptr; |
This code assigns the value of x
(which is 10) to the y
variable.
Pointers can be used in many different ways in C, such as to dynamically allocate memory, to pass arguments to functions by reference, and to work with arrays and strings. They can also be used to create data structures such as linked lists and trees.
C Function Pointer in a While Loop Examples
Here are a few more examples of how you might use a C function pointer in a while
loop:
Example 1:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | #include <stdio.h> // A function that takes an integer and returns an integer int MultiplyByTwo(int x) { return x * 2; } int main() { // Declare a function pointer and initialize it to the address of the // MultiplyByTwo function int (*func_ptr)(int) = &MultiplyByTwo; // Use the function pointer in a while loop int i = 1; while (i <= 10) { printf("%d\n", (*func_ptr)(i)); i++; } return 0; } |
In this example, the func_ptr
variable is a function pointer that points to the MultiplyByTwo
function. The function pointer is used in the while
loop to call the function and print the result. The while
loop will run 10 times, with the value of i
being passed as an argument to the function each time.
This code will output the following:
1 2 3 4 5 6 7 8 9 10 11 12 | 2 4 6 8 10 12 14 16 18 20 |
Function pointers can be useful when you want to call a specific function based on some condition, or when you want to pass a function as an argument to another function. They can also be used to implement function callbacks, where a function is called by another function when a certain event occurs.
Example 2:
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 26 27 28 29 30 31 32 33 34 35 36 37 38 | #include <stdio.h> // A function that takes an integer and returns an integer int MultiplyByTwo(int x) { return x * 2; } // A function that takes an integer and returns an integer int MultiplyByThree(int x) { return x * 3; } int main() { // Declare a function pointer and initialize it to the address of the // MultiplyByTwo function int (*func_ptr)(int) = &MultiplyByTwo; // Use the function pointer in a while loop int i = 1; while (i <= 10) { printf("%d\n", (*func_ptr)(i)); i++; } // Change the function pointer to point to the MultiplyByThree function func_ptr = &MultiplyByThree; // Use the function pointer in a while loop again i = 1; while (i <= 10) { printf("%d\n", (*func_ptr)(i)); i++; } return 0; } |
This code uses a function pointer to call either the MultiplyByTwo
function or the MultiplyByThree
function in a while
loop. The function that is called is determined by the value of the func_ptr
variable.
Example 3:
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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | #include <stdio.h> // A function that takes an integer and returns an integer int MultiplyByTwo(int x) { return x * 2; } // A function that takes an integer and returns an integer int MultiplyByThree(int x) { return x * 3; } // A function that takes a function pointer and an integer and returns an integer int ApplyFunction(int (*func)(int), int x) { return (*func)(x); } int main() { // Declare a function pointer and initialize it to the address of the // MultiplyByTwo function int (*func_ptr)(int) = &MultiplyByTwo; // Use the function pointer in a while loop int i = 1; while (i <= 10) { printf("%d\n", ApplyFunction(func_ptr, i)); i++; } // Change the function pointer to point to the MultiplyByThree function func_ptr = &MultiplyByThree; // Use the function pointer in a while loop again i = 1; while (i <= 10) { printf("%d\n", ApplyFunction(func_ptr, i)); i++; } return 0; } |
This code uses a function pointer as an argument to the ApplyFunction
function, which takes a function pointer and an integer as arguments and returns the result of calling the function pointed to by the function pointer with the given integer. The while
loop calls the ApplyFunction
function with different function pointers to apply different functions to the loop variable.