Pointers – Intro

Hi everyone ✋

 

We will be learning about pointers in depth in this series. At first, it may sound a little confusing but trust me once you get a hang of it you will love it.

Pointer is the fundamental concept of C/C++ programming but we will only be considering C++ forward on.

To understand pointers, first, we need to understand how various data types are stored in a computer’s memory.

We all know that the primary unit of computer memory is the byte. But at the atomic level, even a byte consists of 8 bits where a bit is the smallest unit of data in a computer and can represent a binary value of 0 or 1. When I was on campus I wrote a blog post about it out of passion, you can check it out.

As we know variable name is a piece of memory that holds a value (where the array is a collection of sequential memories). Memory size differs according to the data type ─  

 

Character – char – 1 byte  

Integer – int – 2 or 4 bytes (depends on the compiler)

Floating – float – 4 bytes

Double-sized floating point – double – 8 bytes

 

When we are talking about computer memory, we are talking about RAM. Since it is not possible to draw the whole RAM of the computer. We will draw a chunk for demonstration purposes. Each column of the following table represents 1 byte of memory.

241242243244245246247248249250251252

So, when we are declaring an int  variable, it will take 2-byte from the computer memory


int soudha = 24;

Here, we are declaring a variable named soudha, which takes 2 bytes from computer memory

24
241242243244245246247248249250251252
soudha

Now if we execute the following statements

soudha = 54;
soudha++;

The value of the variable soudha will update as follows

55
241242243244245246247248249250251252
soudha

As we can see, we can only access the value of the variable using the variable name not the assigned address of the variable. To access the address of a variable we need a special kind of variable which is a pointer variable.

That is what we are trying to learn here.

We need to learn about three special kinds of operators before learning about pointer variables.

          • Address-of operator – (&)
          • Dereference operator –  (*)
          • Asterisk operator – (*)

 

Address of operator (&)

The address-of operator is being used to retrieve the address of a variable.

// address-of operator  always return the address of the variable
cout << "address of  soudha is: " << &soudha << endl;

This statement will produce output 242 according to the above picture. Now you can ask why 242? why not 242 or 243 or 242 and 243. When we asked the compiler about the variable’s address it will always return the first byte address. That’s why despite allocating both 242 and 243, it is only returning 242 indexed byte.

The actual output of the compiler will be in hexadecimal format. And the output will be different among different computers since their computer memory is different

 

Dereference operator (*)

We use a dereferencing operator to determine the value stored at a specific memory address. That means dereference operator will always return the stored value of an address. That’s why dereference operator only works with address, otherwise it will give error.

// dereferencing the address value
// here dereferencing operator will work because &soudha is an address
cout << "value of  soudha is: " << *(&soudha) << endl;

This statement will produce output 55 according to the above picture.

 

Asterisk operator (*)

Asterisk operator is the operator which is used to declare pointer type variable. A pointer is a special type of variable that holds another variable’s address. 

Yeah, you heard that right, the pointer is a variable, not an address, a pointer is a special kind of variable that holds other variable addresses.

Now, one question arises which pointer variable will hold which address? Cause there are lots of types of variables, the numbers are limitless because of the feature of user-defined variables such as class and structure. To minimize complexity, just think of the fundamental C++ variables (int, float, double, char, bool).

 


// To declare an int-type pointer we need to execute the following statement
// this ptr variable will point to the int type variable’s address.
int *ptr;

// likewise
float *fPtr; // holds or points to float type variable's address
double *dPointer; // hold or points to double type variable's address
char *cPointer; // hold or points to char type variable's address
bool *bPointer; // hold or points to bool type variables's address

// The size of these pointer variables will be the same as that of their data types.
Another big question can arise: what is this (*) operator?

Is it multiplication, dereference, or asterisk?

I know what we are feeling now. Cause the same operator has three names

* – multiplication, which is a binary operator. Example: x * y

* – dereference, which is a unary operator. Example: *(&soudha)

* – asterisk, which is a unary operator. Example: int *ptr;

It can get confusing. But there is an easy way to differentiate them according to their behavior

 

When we are doing multiplication operations. (*) the operator will be treated as a multiplication operator. 

int x = 5, y = 25;
// here * is the multipication operator
int result = x * y;

When we are trying to determine the value stored at a specific memory address. (*) the operator will be treated as a dereference operator.


// here * is the dereference operator
cout << "value of soudha is: " << *(&soudha) << endl;

When we are declaring a pointer variable. (*) the operator will be treated as an asterisk operator. 


// here * is the asterisk operator
// declaring an int pointer variable with asterisk operator
// asterisk operator only operates when we are declaring a pointer variable
int *ptr;

Now since the confusion clears out. 🥳

Let’s talk pointer variables. 

Wait a bit  🤔

We already talked about pointer when we were learning about the asterisk operator.

Anyway, let’s take a deep dive now 🤿

We already know, how to declare pointers.


// declaring an int-type pointer variable
// we are using asterisk operator to declare pointer variable
int *ptr;

Now, recheck the computer memory.

55
241242243244245246247248249250251252
soudhaptr

As you can see int type pointer variable’s occupied address is 249 and 250 indexed bytes according to the above picture. Memory occupancy of variables is random. Computer OS decides which memory will be stored for which variable.


// So, the output of the following statements will be different and random as per execution
// Also, it will be different on different computers.
cout << "address of ptr is: " << &ptr << endl;

This statement will produce output 249 according to the above picture. As you can see in the above picture pointer variable ptr has no value yet because we did not assign any value to it.

Let’s assign it 🏹


// first declare the int type pointer variable
int *ptr;

// assign the pointer variable to point int variable's address(soudha)
ptr = &soudha;

// or this can be done in one-liner using initialization way
int *ptr = &soudha;

Now, recheck the computer memory.

55242
241242243244245246247248249250251252
soudhaptr

ptr variable is now pointing to the int variable soudha’s memory.


// The following statement output will be 249
// Here address-of operator displays the address of the pointer variable
cout << "address of  ptr is: " << &ptr << endl;

// since, the ptr variable points to the int-type variable soudha’s address
// the following statement's output will be 242
cout << "value of  ptr is: " << ptr << endl;

// Let’s do the same thing using the dereference operator
// the following statement's output will be 242
cout << "value of  ptr is: " << *(&ptr) << endl;

// using dereference to display the value of soudha
// the following statement's output will be 55
cout << “value of soudha is: ” << *ptr << endl;

We can also use the deference operator to change the value of int variable soudha


// changing the value of int variable soudha through pointer variable ptr using dereference
*ptr = 21;
// The following statement's output will be 21
cout << "value of soudha is: " << soudha << endl;

// using dereference to display the value of soudha
// the following statement's output will be 55
cout << "value of soudha is: " << *ptr << endl;

// but pointer variable ptr's pointing won't be changed
// since, the ptr variable is still pointing to the
// int-type variable soudha’s address

// the following statement's output will be 242
cout << "value of  ptr is: " << ptr << endl;

Now, recheck the computer memory.

21242
241242243244245246247248249250251252
soudhaptr

 

Now what if we want to change the address pointing of a pointer variable?  🤔


Very simple, just change the address assignment to a new variable of the same type


// declaring another int variable called imran
int imran = 121;

// assign the imran's address to pointer variable ptr
ptr = &imran;
// Now int type pointer variable ptr is pointing int type variable imran

Now, recheck the computer memory

21121242
241242243244245246247248249250251252
soudhaimranptr

// the following statement's output will be 249
// Here address-of operator displays the address of the pointer variable
// This will be unchanged
cout << "address of ptr is: " << &ptr << endl;

// Since, ptr variable points to the int-type variable imran’s address now
// the following statement's output will be 245
cout << "value of  ptr is: " << ptr << endl;

// let's do the same thing using the dereference operator
// the following statement's output will be 245
cout << "value of  ptr is: " << *(&ptr) << endl;

// now, using dereference to display the value of imran
// The following statement output will be 121
cout << "value of soudha is: " << *ptr << endl;

 

Congratulations 🥳🥳🥳
We completed the very basics of pointers

Now, let’s talk about a common mistake about pointers.

When we are about to change the pointing address of pointer variables. 


// instead of using following statement to assign
ptr = &imran;

// sometimes we use the following statement
 *ptr = &imran;

Which will generate error. But why ?

There is a difference between dereference and asterisk as we already know that from earlier.

When we declare a pointer variable and initialize at the same time, we are using an asterisk operator for that moment


// here, asterisk operator is in use
int *ptr = &imran;

But when we are declaring a pointer and assign it later


// first we declare a pointer variable with asterisk operator
// here, asterisk operator in use
int *ptr;

// but when assigning later we don't need to use
// the asterisk operator again
// because ptr is already declared as a pointer variable
// so it expects an address as when using assignment operation
// and, &imran return the address of int variable imran
ptr = &imran;

Which are perfectly valid statements.

 

But when we are using (*) operator during later assignments, it operates as an dereference operator rather than asterisk operator


// first we declare a pointer variable with asterisk operator
// here, asterisk operator in use
int *ptr;

// but when we are using * operator during assignment
// here, dereference operator is in use
// cause dereference operator only works with address
// and pointer variable ptr itself an address
// when dereference is in use it expects the value store in the address
// since ptr holds or points to int type variable's pointer
// it expecting a int type value in assignment operation
// but &imran is returning the address of int type variable imran
*ptr = &imran;

Since, while dereference operator expecting an int type value but it is getting the address of int type variable, it will show compilation error.

 

Pointers basics are mostly done but let’s talk about an extra.

There is another syntax to declare pointer variables. Actually this is not a separate syntax, this is just different writing style of some coders which also works.


// this is the standard style of pointer variable declaration
// here asterisk is with the variable
int *anik;

// this is the different style of delcaring pointer variable
// here is asterisk with the data-type
int* anik;

 

 

 

 

 

Leave a Comment

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