Address of operator (&) is used to access the address of any variable by prepending the ampersand sign (&) with a variable name. You can see at below example to understand it better.
What is address or memory location of a variable?
In C language, every variable gets a memory location (Address of variable) somewhere in the RAM when it is declared, which help us to access the value of that variable. This memory location has a unique address in hexadecimal format something like 0xbffd8b3c. Simply you can say, A memory location where data is stored is called address of variables.
Address or memory location of variables | welcome2protec.com |
Let’s consider a scenario: In a computer network every computer has a unique IP address if you need data from that computer we need to access that computer by using IP address. The same way if we need to access the value of variable ‘x’ we need to access memory location of that variable by using address of (&) Operator with format specifier either %p, %x or %u.
How to access address or memory location of a variable?
Address of operator (&) is used to specifying the address location of the Variable in C Language.
For Example:
/*while Taking The Input form user */
scanf("%d", &x);
/*Here address of operator (&) Points to the Memory Location of variable x */
Let's say the address assign to variable x is 0xbffd8b3c, which means whatever value will be assigned to x should be stored at the location 0xbffd8b3c. As you can see in the above diagram
As I mentioned at the beginning of this tutorial, Address of operator (&) is used to access the address of any variable by prepending the ampersand sign (&) with a variable name. Now let's see the below example how to access the address of x?
/*Program to print both value and address of variable x */.
int main()
{
int x = 10;
/*Printing the value of variable x*/
printf("Value of x = %d\n" , x);
/*Printing the address of variable x */
printf("Address of x = %u" , &x);
return 0;
}
/* Here address of x will be displayed on the screen.*/
Output:
When the above code is compiled and executed, it produces the following result −
Value of x = 10
Address of x = 0xbffd8b3c
Address of (&) operator also used in passing the reference in the function arguments.
- Visit to learn more about Function call by reference with examples.
//Pass By Reference
void check(int * ,int *); //Function Declaration
// Check is a function taking 2 Arguments pointer to integer.
check(&a,&b); // Function Call
//Here The Passing The Address of a and b to the Function Call.
Points to remember:
- '&' is known as Address of operator or ampersand.
- It is an unary operator.
- '&' is also known as referencing operator.
- Operand must be the name of variable.
- '&' Address of operator returns address of a variable.
0 Comments:
Post a Comment
Please don't enter any spam link in the comment box.