What is & and * operators in C?
C Programming Language > Operators and Enums in C Language > Introduction to Operator
20646
Answer:
- * Operator is used as pointer to a variable. Example: * a where * is pointer to the variable a.
- & operator is used to get the address of the variable. Example: &a will give address of a.
Explanation
"&"
operator in C is the "address of" operator. It is used to obtain the memory address of a variable. For example, if you have an integer variable "x" and you want to know its memory address, you would use "&x".
"*"
operator in C is the "dereference" operator. It is used to obtain the value stored at a memory address. For example, if you have a pointer variable "p" that points to an integer and you want to know the value stored at that memory address, you would use "*p". This operator is also used in pointer declaration and function argument.
Practical Example
The address-of operator (&) in C is used to get the memory address of a variable. Here is an example:
#include int main() { int x = 5; printf("The memory address of x is: %p\n", &x); return 0; }
In this example, the variable x is initialized with the value of 5, and the address of x is printed using the address-of operator and the %p format specifier. The output would be something like "The memory address of x is: 0x7ffc6f9a6b24"
The dereference operator * in C is used to get the value stored at a memory address. Here is an example:
#include int main() { int x = 5; int *ptr = &x; printf("The value of x is: %d\n", *ptr); return 0; }
In this example, the variable x is initialized with the value of 5, the memory address of x is stored in ptr and finally, the value stored at the memory address of x is printed using the dereference operator * and the %d format specifier. The output would be something like "The value of x is: 5"
This Particular section is dedicated to Question & Answer only. If you want learn more about C Programming Language. Then you can visit below links to get more depth on this subject.
Join Our telegram group to ask Questions
Click below button to join our groups.