16 – Pointers in C Programming Language

In this chapter, we will talk about Pointers.

In C, Pointers are very easy to learn. Some C programming Tasks are performed more easily with Pointers & other tasks such as dynamic memory location cannot performed without using Pointer. So it becomes necessary to learn Pointers to become a perfect C programmer.
So Let’s start learning Pointers in simple & easy way.

As we know that, every variable is a memory location & every memory location has its defined address which can be accessed using ampersand (&) operator, which denotes an address in a memory. Let’s consider the following example, Which prints the address of the defined variables:

When the above code is compiled & executed, it produces the following result:

What are Pointers?

A pointer is a variable whose value is the address of another variable i.e. direct address of the memory location. Like any variable or constant, you must declare a pointer before using it to store any variable address.
The general form of pointer variables declaration is:

Here, type is pointer base type. It must be a valid C data type & var-name is the name of the pointer variable. However, in this statement the asterisk ( * ) is used to designate a variable is a pointer. Just take a look at some of the valid pointer declarations:

The actual data type of the value of all pointers, whether integer, float, character is the same, a long hexadecimal number which represents a memory address. The only difference between pointer of different data type is the data type of the variable or constant.

How to use Pointers?

There are few important operation, which we will do very frequently with the help of Pointers.

(a) Define a pointer variable
(b) Assign the address of a variable to a pointer
(c) access the value at the address variable in the pointer variable

This is done by unary operator ( * ) that returns the value of the variable located at the address specified by its operand.
The following example shows the use of these operations:

When the above code is compiled & executed, it produces the following result:

Null Pointers

It is a good practice to assign a Null value to a pointer variables, in the case you do not have an exact address to be assigned, this is done at the point of variable declaration. A pointer that is assigned a Null value is called a null pointer.

The Null pointer is a constant with a value of zero defined in several standard libraries.
Consider the following program:

When the above code is compiled & executed, it produces the following result:

In most Operating systems, programs are not permitted to access memory at address at 0 because that memory is reserved by the OS. However, the address memory 0 has special significance; it signals that the pointer is not intended to point to an accessible memory location, But by convention, if a pointer contains a Null (zero) value, it is assumed to point to nothing.

To check for a Null pointer, you can use an ‘ if ‘ statement as follows:

Pointers in Detail

Pointers have many easy concepts & they are very important to C programming. The following important pointer concepts should be clear to any C programmer:

Sr.NoConceptDescription
1Pointer ArithmeticThere are 4 Arithmetic Operators that can be used in Pointers: +, -, ++, —
2Array of PointersWe can define arrays to hold a number of pointers.
3Pointer to PointerC allows us to have pointer to an pointer & so on.
4Passing Pointers to Functions in CPassing an argument by reference or by an address enable the passed argument to be changed in the calling function by the called function.
5Return Pointers from Functions in CC allows a function to return a pointer to the local variable, static variable & dynamically allocated memory as well.

Subscribe Our channels:

Click Here for more Articles:

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.