C Program to play with variables and their addresses

Pointers in C++

Pointers in C++

This is a simple C program to show pointers and how you can use pointers to print address of variables and their contents.

This program prints the address of a variable, value of a variable, values of pointers.

#include <stdio.h>

void moo(int a, int * b);

int main(void) {
  int x;
  int *y;

  x=1;
  y=&x;
  printf("Address of x = %d, value of x = %d\n", &x, x);
  printf("Address of y = %d, value of y = %d, value of *y = %d\n", &y, y, *y);
  moo(9,y);
}

void moo(int a, int *b){
  printf("Address of a = %d, value of a = %d\n", &a, a);
  printf("Address of b = %d, value of b = %d, value of *b = %d\n", &b, b, *b);
}

 

Output from running this C program on my computer:
Address of x = 536869640, value of x = 1
Address of y = 536869632, value of y = 536869640, value of *y = 1
Address of a = 536869608, value of a = 9
Address of b = 536869600, value of b = 536869640, value of *b = 1

 

M. Saqib: Saqib is Master-level Senior Software Engineer with over 14 years of experience in designing and developing large-scale software and web applications. He has more than eight years experience of leading software development teams. Saqib provides consultancy to develop software systems and web services for Fortune 500 companies. He has hands-on experience in C/C++ Java, JavaScript, PHP and .NET Technologies. Saqib owns and write contents on mycplus.com since 2004.
Related Post