Learning Sections show
Pointers in C
Example
#include<stdio.h> // Include the standard input-output library
int main() {
// Variable declaration
int var = 10;
// Pointer declaration
int *ptr;
// Assigning the address of var to ptr
ptr = &var;
// Print the address stored in ptr
printf("Address of var: %p\n", ptr);
// Print the value of var using the pointer
printf("Value of var: %d\n", *ptr);
return 0;
}
Explanation:
Pointers in C are variables that store the address of another variable. They are powerful tools for managing memory and manipulating data structures.
In this example:
#include<stdio.h>
: Includes the standard input-output library which allows us to use theprintf
function.int main()
: The main function where the program execution begins.int var = 10;
: Declares an integer variablevar
and initializes it with the value 10.int *ptr;
: Declares a pointer to an integer.ptr = &var;
: Assigns the address of the variablevar
to the pointerptr
.printf("Address of var: %p\n", ptr);
: Prints the address stored in the pointerptr
.printf("Value of var: %d\n", *ptr);
: Prints the value of the variablevar
by dereferencing the pointerptr
.return 0;
: Ends the main function and returns 0 to the operating system, indicating that the program ended successfully.
In summary, pointers in C allow you to directly access and manipulate memory addresses. They are used in various applications, such as dynamic memory allocation, array manipulation, and function argument passing.