Call us at 0700-922-6559 or Click here

Write a program to display address of variable using pointers.

In programming, a variable is a named location in memory that holds a value. Every variable in a program has a unique address in memory that identifies its location. A pointer is a special type of variable that stores the memory address of another variable.

Below is the program to display address of variable using pointers with explanation.

To display the address of a variable using pointers, we can write a program that does the following:

  1. Define a variable of a suitable data type, such as an integer or a character.
  2. Declare a pointer variable of the same data type as the variable we want to display the address of.
  3. Assign the address of the variable to the pointer using the & operator.
  4. Use the printf() function to display the address of the variable by dereferencing the pointer variable using the * operator.

Here is an example program in C that demonstrates how to display the address of a variable using pointers:

#include <stdio.h>

int main() {
int num = 10;
int *ptr = &num;

printf("The address of the variable num is: %p", ptr);

return 0;

}

Output-

The address of the variable num is: 0x7fff5fbff830

In this program, we first define an integer variable num and assign it a value of 10. Then, we declare a pointer variable ptr of type int and assign the address of num to it using the & operator.

Finally, we use the printf() function to display the address of num by dereferencing ptr using the * operator and formatting the output with %p.

When we run this program, it will display the memory address of the num variable on the screen, which should look something like 0x7fff5fbff830 (the exact address will depend on your system).

Overall, this program demonstrates how to use pointers to display the memory address of a variable in memory.

Once you have written the code, you can compile and run it using a C compiler, such as GCC, on your computer. The program will then display output on the screen when executed.

If you want to learn more about C programming, then join our course.

Leave a Reply

Your email address will not be published. Required fields are marked *