Skip to main content

Command Palette

Search for a command to run...

Pointers Made Simple: Your Guide to Mastering Memory Management

Updated
β€’6 min read
Pointers Made Simple: Your Guide to Mastering Memory Management

Before understanding Pointers, first, let's understand what is memory address πŸ€”

Address-

Address or reference is a position number of bytes in the program's memory.

Let's visualise it.

Ok, when we declare a variable in the C program.

int x;

What's going on behind the scenes?

As you can see in the image four bytes are allocated for the x variable as it is an int type variable.

but what are the numbers??

The numbers are the addresses of each byte, but remember these numbers may be different in your compiler and generally we need only the address of the first byte to know where is x because the other four numbers are consecutive. -> remember this

Referencing and Dereferencing operator

int x;
x=5;
printf("%d",x);
printf("%d",&x);
printf("%d",*&x);

let's visualise this code.

so a variable is declared x. which is containing value 5 whose address is 1000 (for example) always remember you cannot decide or change the address of any variable.

but what is & and * ???

let's talk about &-

it is an operator which helps you to access the address of a variable. It is called Referencing Operator.

let's talk about *-

It is an indirection operator that allows you to obtain the value of a variable by providing its address. For example, if you print *1000, it will give you the value 5. However, as I mentioned earlier, the number 1000 can vary every time we run the program. So, how can we write the address?

Yes, we have learned to access the address of a variable by using the '&' symbol. Now, if we want to get the value of 5, we can print *&x where &x will give us the address, and then with the help of the * operator, we can retrieve the value of x.

* is called Dereferencing operator.

let's run the program.

Output-

5
6422300
5

here you can see in my system the address of x is 6422300, it can be different from you. Observe all three outputs.

Ok, so always remember &x is not a variable, is just a way to represent the address of variable x. The address number is a constant value.

So let's learn how to store values of address. We can not store addresses in normal variables.

Let's do it!!

int *j;

j will store the address of the x variable

let's write a simple code.

int x=5;
int *j;
j=&x;
printf("%d %d %d", &x,j,*&x);

let's visualise it-

here we can see 5 is stored in the variable i and the address of i which can be anything let's say 1000 is stored in j and j is also having its address let's say 2000 and is called a pointer variable.

Let's check the output-

6422296 6422296 5

check the first output which is the address of x (&x), talking the second output is also the address of x (j) and the last output is 5 which is the value of x.

Now let's learn about Pointer

What is Pointer?

Very simple, The pointer is a variable which contains the address of another variable.

Everything we've talked about so far will start to make sense, and get ready for an exciting and enjoyable exploration of pointers that will completely demolish any fear you might have had about them!

Size of Pointer

int a,*p;
char b, *q;
  • a,b are ordinary variables

  • p,q are pointer variables which will store the address

  • 'a' having four bytes (because of int type)

  • 'b' having 1 byte ( because of char type)

but

As you can see, both 'p' and 'q' have 8 bytes of memory, whether you declared them as int or char. This is because when you store the address of a variable (like 'b') in 'q', the address itself is a number that takes up 8 bytes of memory. So, it doesn't matter if 'q' is declared as int or char, it will always occupy 8 bytes of memory to hold the address.

so this was all about the size of a pointer

Now let's talk about the base address.

Base Address

Address of the first byte of any variable.

Let's visualize it...

int a,*p;
char b, *q;
p=&a;
p=&b;

Variable 'a' is of type 'int', which means it occupies 4 bytes in memory, and each byte has a consecutive address (1000, 1001, 1002, 1003). When we store the address of 'a' in the pointer 'p', it holds the address of the first byte of 'a' (address 1000). Since the bytes are arranged consecutively, 'p' can automatically access the other three bytes by following the consecutive addresses. This allows 'p' to access the entire 'a' variable in memory.

Let's talk about a Bug

A pointer should be of the same type as the type of variable whose address you want to store.

for example -

int a,*p;
char b, *q;
p=&a;
p=&b;

if you are declaring a pointer variable in int type, then use that pointer variable to store the address of the int type variable, for example in this case when you are declaring pointer variable 'p' in int type then use it to store the address of int type variable which is a.

Let's see what will happen when you store the address of b (which is char type ) in 'p' (which is int type)

int a,*p;
char b, *q;
p=&b;

it will not show an ERROR!

But you should not do this

Why??

let's visualise it

Keep in mind that a pointer variable always stores the base address of a variable. If you store the base address of a variable of type 'char' in an 'int' type pointer variable, it will mistakenly consider that there are three additional bytes. Consequently, when you run the program, it might work correctly sometimes, but at other times, the program may crash due to illegal access of those extra three bytes.

Thank you for taking the time to read the blog. Your interest and engagement are greatly appreciated!!

H

Great work πŸ‘πŸ‘

2