1.) & Operator (Address-of Operator):
The & operator is used to get the address of a variable in memory.
Thank you for reading this post, don't forget to subscribe!- It returns the memory location of the variable.
- It is mainly used to assign the address to a pointer.
Syntax:
&variable_nameExample:
#include <stdio.h>
int main() {
int a = 5;
printf("Value of a: %d\n", a);
printf("Address of a: %p\n", &a); // &a gives the memory address of variable a
return 0;
}2.) * Operator (Dereference Operator):
The * operator is used to access the value at a given address. It is also used to declare a pointer.
Syntax:
*pointer_nameExample:
#include <stdio.h>
int main() {
int a = 10;
int *ptr = &a; // ptr stores address of a
printf("Value of a: %d\n", a);
printf("Address of a: %p\n", ptr); // Using pointer
printf("Value at ptr: %d\n", *ptr); // Dereferencing the pointer
return 0;
}