Pointer declaration in C language.

How does Address Operator work in C?

In C programming, the address operator, represented by the "&" sign, works by returning the memory address of a variable. When you use the address operator with a variable, it retrieves the location in memory where that variable is stored. This memory address is a unique identifier for the variable's location in the computer's memory.

& in scanf(): By using the `&` address of operator in `scanf`, you are essentially passing a pointer to the variable's memory location where the input value should be stored, enabling `scanf` to update the variable's value directly in memory.
Syntax:
//stores the value of the variable
scanf("%d",&variable_name);
Input or store values using & address of operator in scanf().

Example 1. C program for Scanning user integer input with the ampersand and display the input.

//include is used to add basic C libraries
#include<stdio.h>
int main()
 {
  //declaring variables
  int n1, n2;
  //Asking user to enter integer input
  printf("Please enter first integer number \n");
  scanf(“%d”,&n1);
  printf("Please enter second integer number \n");
  scanf("%d”,&n2);
  //displaying output
  printf("\n The Numbers are : %d and %d ", n1, n2);
  return 0;
 }

Output:
Please enter first integer number
5
Please enter second integer number
10
The Numbers are : 5 and 10


Example 2. C program for Scanning user String input with ampersand & and display

#include <stdio.h>
int main()
 {
  char fname[30],lname[20];
  //Ask user to input first name
  printf("Please enter your first name = ");
  //Store the fname in variable fname
  scanf("%s",&fname);
  //Ask user to input last name
  printf("Please enter your last name = ");
  //Store the fname in variable lname
  scanf("%s",&lname);
  //displaying output
  printf("Your name is %s %s ", fname,lname);
  return 0;
 }
  Output:
  Please enter your first name =Ajay
  Please enter your last name =Devgan
  Your name is Ajay Devgan


Example #3 Address of operator(&) and dereference operator (*) in C

#include <stdio.h>
int main(void)
 {
  //declaring variables
  int *p;
  int **q;
  int n;
  //Asking user to enter input
  printf("Please enter a number = ");
  //Store the number at variable n(at the address of n)
  scanf("%d",&n);
  //take the address of the n into the p single pointer
  p=&n;
  //take the address of the n into the q double pointer, it will give the address of the address
  q=&n;
  //displaying output to the screen to end user
  //address output may vary from compiler to compiler
  printf("Value of n is =%d \n",n);
  printf("Address of *p is %x\n", &p);
  printf("Address of **q is %x", &q);
  return 0;
 }

Output:
Please enter a number =5
Value of n is =5
Address of *p is=5000
Address of **q is=5004

Note: Address may vary from system to system.

Previous Topic:-->> Application or use Pointers in C. || Next topic:-->>Complex Pointer in C.