Pointers
● A pointer can be used to store the memory address of other variables, functions, or
even other pointers.
● The use of pointers allows low-level memory access, dynamic memory allocation, and
many other functionality in C.
Demo1:
#include <stdio.h>
int main()
{
int var = 20; // actual variable declaration
int *ptr; // pointer variable declaration
ptr = &var; // store address of var in pointer variable
printf("Address of var variable: %p\n",&var);
printf("Address stored in ptr variable: %p\n",ptr);
printf("Value of *ptr variable: %d\n", *ptr);
return 0;
}
Demo2:
#include <stdio.h>
void display()
{
int var = 10; // declare pointer variable
int* ptr; // note that data type of ptr and var must be same
ptr = &var; // assign the address of a variable to a pointer
printf("Value at ptr = %p \n", ptr);
printf("Value at var = %d \n", var);
printf("Value at *ptr = %d \n", *ptr);
}
int main() {
display();
return 0;
}