Pointers : In Simple Terms

Before starting a pointer first clear basic concepts of a variable.

A variable is nothing but a name given to a storage area that our program can manipulate. Each variable in the C has a specific type which determines a size and the layout of the variable’s memory; the range of values that can be stored within that memory; and the set of operations that can be applied to the variable.

When we declare a variable like

int no = 21;

Then it does get 4 bytes of memory and inside that memory 21 is stored.

What are Pointers?

Different from other normal variables which can store the values, pointers are the  special variables that can hold an address of variable. Since they store memory address of variable, the pointers are generally said to be “point to variables”.

Pointer C

Var                                        Ptr

(normal variable)           (pointer)

As shown in the above diagram:

A normal variable ‘var’ has a memory address of 1001 and holds a value 50.

A pointer variable has its own address 2047 but stores 1001, which is the address  of the variable ‘var’.

How to Declare a Pointer?

A pointer is declared as :

<pointer type> *<pointer-name>

In the above declaration :

1. pointer-type : It specifies the type of pointer.

It can be int,char, float etc. This type specifies the type of variable whose address this pointer can store.

This is important because when we derefer the pointer then that pointer can access on sizeof pointer type bytes.

Means if pointer is double pointer, that pointer can access only 8 bytes.

All the pointer arithematics are depend on this type.

2.pointer-name : It can be any name specified by user. Professionally, there

are some coding styles which every code follows. The pointer names commonly start with ‘p’ or end with ‘ptr’

An example of a pointer declaration can be :

char *chptr;

In above declaration, ‘char’ signifies the pointer type, chptr is the name of the pointer while the asterisk ‘*’ signifies that ‘chptr’ is a pointer variable.

How to initialize a Pointer?

A pointer is initialized in the following way :

<pointer declaration(except semicolon)> = <address of a variable>

OR

<pointer declaration>

<name-of-pointer> = <address of a variable>

Note that the type of  a variable above should be same as the pointer type.

For example :

char ch = ‘c’;

char *chptr = &ch; //initialize

OR

char ch = ‘c’;

char *chptr;

chptr = &ch //initialize

In the code above, we declared a character variable ch which stores the value ‘c’. Now, we declared a character pointer ‘chptr’ and initialized it with the address of variable ‘ch’.

Note that the ‘&’ operator is used to access the address of any type of variable.

How to Use a Pointer?

A pointer can be used in two contexts.

Context 1:

For accessing an address of a variable whose memory address the pointer stores.

Again consider the following code :

char ch = ‘c’;

char *chptr = &ch;

Now, whenever we refer the name ‘chptr’ in a code after above two lines then compiler will try to fetch the value contained by this pointer variable, which is the address of the variable (ch) to which the pointer points. i.e. the value given by ‘chptr’ would be equal to ‘&ch’.

For example :

char *ptr = chptr;

The value held by ‘chptr’ (which in this case is the address of the variable ‘ch’) is assigned to the new pointer ‘ptr’.

Context 2:

For accessing a value of variable whose memory address the pointer stores.

Continuing with the piece of code used above :

char ch = ‘c’;

char t;

char *chptr = &ch;

t = *chptr;

We can see that in the last line above, we have used ‘*’ before the name of the pointer.

 

 

What does this asterisk operator do?

This operator when applied to a pointer variable name(like in the last line above) yields the value of the variable to which this pointer points. Which means, in this case ‘*chptr’ would yield the value kept at address held by chptr. Since ‘chptr’ holds the address of variable ‘ch’ and value of ‘ch’ is ‘c’, so ‘*chptr’ yeilds ‘c’.

When used with pointers, the asterisk ‘*’ operator is also known as ‘value of’ operator.

For more reading about technology news in singapore and seo to online marketing do view more about other pages.

Sourabh Bhunje

Sourabh Bhunje, B.E. IT from Pune University. Currently Working at Techliebe. Professional Skills: Programming - Software & Mobile, Web & Graphic Design, Localization, Content Writing, Sub-Titling etc. http://techliebe.com/about-us

Leave a Reply