The address and indirection operators

Now we will consider how to determine the address of a variable. The operator that is available in C for this purpose is “&” (address of ) operator. The operator & and the immediately preceding variable returns the address of the variable associated with it. C’s other unary pointer operator is the “*”, also called as value at address or indirection operator. It returns a value stored at that address. Let us look into the illustrative example given below to understand how they are useful.

Write a program to print the address associated with a variable and value stored at that address.

/* Program to print the address associated with a variable and value stored at that address*/
# include <stdio.h>
main( )
{
int qty = 5;
printf ("Address of qty = %u\n",&amp;qty);
printf ("Value of qty = %d \n",qty);
printf("Value of qty = %d",*(&amp;qty));
}

OUTPUT

Address of qty = 65524
Value of qty = 5
Value of qty = 5

Look at the printf statement carefully. The format specifier %u is taken to increase the range of values the address can possibly cover. The system-generated address of the variable is not fixed, as this can be different the next time you execute the same program. Remember unary operator operates on single operands. When & is preceded by the variable qty, has returned its address. Note that the & operator can be used only with simple variables or array elements. It cannot be applied to expressions, constants, or register variables.

Observe the third line of the above program. *(&qty) returns the value stored at address 65524 i.e. 5 in this case. Therefore, qty and *(&qty) will both evaluate to 5.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: