Address Operator In C

The syntax for the address operator in C programming language is as follows:

Example

&variable_name;

In this scenario, the variable that requires obtaining its address is referred to as "variable_name".

Let's examine a demonstration to gain a deeper understanding of how the address operator is utilized.

Example

#include <stdio.h>

int main () {
int num = 10;
printf("The address of num is %p", &num);
return 0;
}

Output:

The program's output would look like this:

Example

The address of num is 0x7fff5fbff7ac

Explanation:

In the aforementioned code snippet, we defined an integer variable named "num" and assigned it the value 10. Subsequently, we employed the printf function to display the memory address of the "num" variable. The memory address is exhibited utilizing the '%p' format specifier in hexadecimal representation.

The program displays the memory address of the variable 'num' as '0x7fff5fbff7ac' during its execution.

Address Operator Uses:

  1. Passing Pointers as Function Arguments:

Using pointers as function arguments is a primary use case of the address operator. Pointers, which are variables that store memory addresses of other variables, enable the modification of the original value of a variable from the calling function by passing the variable's address as a parameter to a function.

Example

#include <stdio.h>

void swap (int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}

int main() {
int x = 10, y = 20;
printf("Before swapping: x = %d, y = %d\n", x, y);
swap(&x, &y);
printf("After swapping: x = %d, y = %d\n", x, y);
return 0;
}

Output:

The program's output would look like this:

Example

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

Explanation:

The function "swap" has been defined in the preceding code, taking two integer pointers as parameters. Inside the function, we retrieved the values stored at the memory addresses pointed to by the pointers by using the address operator. Subsequently, we interchanged the values of the variables using a temporary variable.

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.

  1. Dynamic Memory Allocation:

The address operator is also applied in dynamic memory allocation. By utilizing the malloc function, dynamic memory allocation allows for memory allocation during runtime. The address operator is employed to retrieve the memory block assigned by the pointer returned by the malloc function.

Example

#include <stdio.h>
#include <stdlib.h>

int main() {
    int *arr;
    int n, i;

printf("Enter the number of elements: ");
scanf("%d", & n);

arr = (int*) malloc(n * sizeof(int));

if(arr == NULL) {
printf("Memory allocation failed.");
    return 0;
}

for(i = 0; i< n; i++) {
printf("Enter element %d: ", i+1);
scanf("%d", &arr[i]);
}

printf("The elements of the array are: ");
for(i = 0; i< n; i++) {
printf("%d ", arr[i]);
}

free(arr);
return 0;
}

Output:

The program's output would look like this:

Example

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 mentioned software, we've initialized variables 'n' and 'i' along with an integer type pointer 'arr'. The user will be asked to input the number of elements in the array using the printf function.

We employed the malloc function to reserve memory for the variable "arr". Each integer element's size within the array is determined by using the 'sizeof(int)' parameter. In case the memory allocation is unsuccessful, the program terminates after presenting an error notification.

To collect input from the user for the array's elements, a for loop was employed. Each element within the array can be retrieved by leveraging the address operator. Another for loop was used to display the array's elements on the screen.

Input Required

This code uses input(). Please provide values below: