CodeSteps

Python, C, C++, C#, PowerShell, Android, Visual C++, Java ...

C Programming – Understanding of pointers in C

Pointers are address holders, points to something. Each variable or function in the C language has an address. When a variable is defined, it occupies some memory, in the process memory space. When we assign a value to the variable, the value will be stored in the address of the variable. Through this article, we are going to discuss, dealing with Pointers in C.

Defining a pointer variable

Asterisk (*) symbol is used to define a pointer variable. Yes, it is the same as the multiplication operator (*); but the meaning of it will depend on where we use this symbol. When we use it at the time of defining the variable, that means, we are defining a pointer variable. Here is an example;

int *pi;

The above statement, defines a pointer variable, pi of the type int *. Does the pointer variable have a type? Yes. It has a type. Usually, that depends on the type of the variable, and whose address going to be stored.

It is always a good practice, to set the pointer to a null or 0, value; if you are not assigning an address to it, at the time of defining the pointer variable. So, we can write the above statement as;

int *pi = 0;

Another important thing is, that you need to be cautious when you define multiple pointer variables on the same line. For example;

int* pi = 0, pj = 0;

Most of us think that the above statement creates two pointer variables; pi & pj. But this is not correct. Only pi is the pointer variable and pj is a normal variable of int type. To make pj as a  pointer variable, we must have to add * to it. That means, we can write the above statement to create two pointer variables, as;

int *pi = 0, *pj = 0;

Address-of (&) operator or Reference operator

Once we declare the pointer variable, we have to assign an address to it. Address-of (&) operator is used to get the address of the variable. Do not confuse with bit-wise AND (&) operator. Bit-wise AND (&) operator works on two operands, whereas the address-of operator works on a single operand.

Using Address-of (&) operator, we will get the address of the variable and assign this to the pointer variable. Here is the code;

int i = 20;
int *pi = &i;

After the above statement, the pointer variable pi, holds the address of the variable i. Remember that, pi does not contain, the value of the variable i; it contains its address.

Let’s print the value & address of the variable i;

printf("Variable \"int i\" has the value %d\n", i);
printf("Address of it is, 0x%x\n", pi);

The output looks like the below;

Variable "int i" has the value 20
Address of it is, 0x6ff9e8

Indirection or De-reference (*) operator

Asterisk (*) symbol is used to retrieve the data from the pointer variable. Again, do not confuse with the multiplication (*) operator; which applies to two operands; whereas the indirection operator applies to a single operand.

As mentioned earlier, depending on the context, the meaning of the symbol (*) varies. We use this to declare the pointer variable, access the data from the pointer variable, and also as a multiplication operator. So, do not confuse the way to use this operator.

i & *pi returns the value of the variable i. As pi holds the address of the variable i, we can modify the value of i through pi, like below;

*pi = 100;

Now the value in the variable i will be 100.

Why do we need pointers?

Pointers are needed wherever references are needed. Sometimes it is required to modify or access the data through references, instead of using direct variables. The best examples are, passing a function to another function, modifying the content of passed variables within the function, returning multiple values from the function, etc,.

When we represent the pointers, in a picture, it looks something like this;

Pointers representation in C
Pointers representation in C

// Malin

C Programming – Understanding of pointers in C

2 thoughts on “C Programming – Understanding of pointers in C

Leave a Reply

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

Scroll to top