Javatpoint Logo
Javatpoint Logo

Address operator in C

C's Address operator is a strong tool that gives programmers direct access to memory locations. It is represented by the '&' ampersand sign. The address operator is a unary operator that gives back a variable's memory address.

Syntax:

The following is the syntax for the address operator in C:

In this case, the variable whose address needs to be obtained is called "variable_name".

Let's look at an example to further comprehend how the address operator is used.

Output:

The program's output would look like this:

The address of num is 0x7fff5fbff7ac

Explanation:

In the above program, we declared the integer type variable "num" and initialized it with the number 10. We used the printf() function to output the variable "num"'s memory address. The memory address is printed using the '%p' format specifier in hexadecimal format.

The memory location of the variable 'num' is displayed on the screen when the program is run. The memory address in this instance is '0x7fff5fbff7ac'.

Address Operator Uses:

1. Passing Pointers as Function Arguments:

Passing pointers as function parameters is one of the address operator's main applications. Pointers are variables that keep track of other variables' memory addresses. The original value of a variable in the calling function can be changed by supplying the variable's address as a function argument.

Output:

The program's output would look like this:

Before swapping: x = 10, y = 20
After swapping: x = 20, y = 10

Explanation:

The function "swap" has been declared in the above program, and it accepts two integer pointers as inputs. We have accessed the values of the variables that the pointers pointed to within the function using the address operator. After that, using a temporary variable, we switched the variables' values.

Two integer variables named 'x' and 'y' have been defined in the main function, and they have each been given an initial value of 10 and 20 respectively. The values of the variables both before and after using the 'swap' function were printed using the printf() function. The 'swap' function has been given the addresses of 'x' and 'y' as arguments.

2. Dynamic Memory Allocation:

The address operator is also used in dynamic memory allocation. Using the malloc() function, dynamic memory allocation enables run-time memory allocation. The address operator can be used to access the memory block that was allocated using the pointer that the malloc() function returns.

Output:

The program's output would look like this:

Enter the number of elements: 5 
Enter element 1: 10
 Enter element 2: 20 
Enter element 3: 30 
Enter element 4: 40 
Enter element 5: 50 
The elements of the array are: 10 20 30 40 50

Explanation:

In the aforementioned program, we have declared variables 'n' and 'i' as well as a pointer 'arr' of integer type. The user will be prompted to provide the array's element count using the printf() method.

We used the malloc() function to allocate memory to the reference "arr". The size of each integer element in the array is specified by the 'sizeof(int)' option. The program exits after displaying an error message if the memory allocation fails.

To read the array's elements from the user, we utilized a for loop. Each element of the array can be accessed using the address operator. We utilized another for loop to print the array's elements on the screen.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA