C Programming – Understanding of pointers in C

Pointers are address holders, points to address of something. Each variable or a function in C language, has an address. When a variable is defined, it occupies some memory, in the process address 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 same as multiplication operator (*); but the meaning of it will be depend on where we use this symbol. When we use at the time of defining the variable, that means, we are defining a pointer variable. Here is an example;

int *pi;

Above statement, defines a pointer variable, pi of type int *. Does pointer variable has a type? Yes. It has a type. Usually, that depends on the type of variable, 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, 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 thinks that, the above statement creates two pointer variables; pi & pj. But this is not correct. Only pi is the pointer variable and pj is normal variable of int type. To make pj as 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 address-of operator works on 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 not contains, the value of the variable i; it contains it’s 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 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 multiplication (*) operator; which applies on two operands; where as indirection operator applies on single operand.

As mentioned earlier, depending on the context, the meaning of the symbol (*) varies. We use this to declare the pointer variable, to access the data from the pointer variable and also it acts 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 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. 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

One Comment

Add a Comment

Your email address will not be published.