C - Pointers and Variables Memory Representation

C Programming


In this tutorial we will learn about pointers and variables representation in memory in C programming language.

In the previous tutorial Pointers and Variables we learned to manipulate variables via pointers. Now, lets talk about the memory representation of the variables and pointers.

Creating variables

For this tutorial we will consider the following four variables of data types char, int, float and double.

Data Type Variable Memory Size
char char ch = 'a'; 1 byte
int int i = 10; 2 bytes
float float f = 12.34; 4 bytes
double double d = 12.3456; 8 bytes

We can represent the above variables as follows.

Points to note!

  • In the above image the values of the variables are stored in binary form i.e., as 0s and 1s (not shown in the image).
  • Memory addresses are in decimal form. We can also use hex form like 0x03E8 for 1000.
  • Each block of memory is of size 1 byte.

To keep things simple we are taking the starting addresses as 1000, 2000, 3000 and 4000.

So, char ch = 'a'; variable gets the memory address 1000 and it is of size 1 byte.

int i = 10; variable gets the address 2000 and 2001 as it is of 2 bytes.

float f = 12.34; variable gets the address 3000 to 3003 as it is of 4 bytes.

double d = 12.3456; variable gets the address 4000 to 4007 as it is of 8 bytes.

Size of pointer variables

Pointer variables stores the address of other variables. And these addresses are integer value.

We can use the sizeof() operator to find the size of the pointer variable.

The size of the pointer variables depends on the compiler. For example, Borland C/C++ compiler takes 2 bytes to save integer value so, pointer size will be 2 bytes. Whereas, Visual C++ compiler takes 4 bytes to save integer values so, in that case size of the pointer will be 4 bytes.

Pointers for the variables

We will create four pointer variables for the four variables.

char *chPtr = &ch;
int *iPtr = &i;
float *fPtr = &f;
double *dPtr = &d;

We can represent the above pointer variables as follows.

In the above image we are assuming that an integer value takes 2 bytes. So, pointers variable size is 2 bytes.

So, pointer variable char *chPtr = &ch; gets the address 8000 and 8001. We are just showing the starting address 8000. The value stored in these memory locations is the starting location 1000 of the character variable ch.

Pointer variable int *iPtr = &i; gets the address 8002 and 8003. We are just showing the starting address 8002. The value stored in these memory locations is the starting location 2000 of the integer variable i.

Pointer variable float *fPtr = &f; gets the address 8004 and 8005. We are just showing the starting address 8004. The value stored in these memory locations is the starting location 3000 of the float variable f.

Pointer variable double *dPtr = &d; gets the address 8006 and 8007. We are just showing the starting address 8006. The value stored in these memory locations is the starting location 4000 of the double variable d.

Complete Code

#include <stdio.h>

int main(void) {
  // variables
  char ch = 'a';
  int i = 10;
  float f = 12.34;
  double d = 12.3456;
  // pointers
  char *chPtr = &ch;
  int *iPtr = &i;
  float *fPtr = &f;
  double *dPtr = &d;
  // print value
  printf("ch: %c\n", *chPtr);
  printf("i: %d\n", *iPtr);
  printf("f: %f\n", *fPtr);
  printf("d: %lf\n", *dPtr);

  // print address
  printf("Address ch: %ld\tsizeof ch: %ld\n", &ch, sizeof(ch));
  printf("Address i: %ld\tsizeof i: %ld\n", &i, sizeof(i));
  printf("Address f: %ld\tsizeof f: %ld\n", &f, sizeof(f));
  printf("Address d: %ld\tsizeof d: %ld\n", &d, sizeof(d));
  printf("Address chPtr: %ld\tsizeof chPtr: %ld\n", &ch, sizeof(chPtr));
  printf("Address iPtr: %ld\tsizeof iPtr: %ld\n", &i, sizeof(iPtr));
  printf("Address fPtr: %ld\tsizeof fPtr: %ld\n", &f, sizeof(fPtr));
  printf("Address dPtr: %ld\tsizeof dPtr: %ld\n", &d, sizeof(dPtr));
  return 0;


ch: a
i: 10
f: 12.340000
d: 12.345600

Address ch: 140732808304699	sizeof ch: 1
Address i: 140732808304692	sizeof i: 4
Address f: 140732808304688	sizeof f: 4
Address d: 140732808304680	sizeof d: 8

Address chPtr: 140732808304699	sizeof chPtr: 8
Address iPtr: 140732808304692	sizeof iPtr: 8
Address fPtr: 140732808304688	sizeof fPtr: 8
Address dPtr: 140732808304680	sizeof dPtr: 8

Note! In the above output we can see the size of the data types and the pointer variables. The compiler I am using is using 8 bytes to store integer values for pointer variables.

Address and size of the data types and pointers will vary depending on the compiler you are using.

Thanks for reading. See you in the next tutorial. Have fun :-)