C language – Full Explanation of Pointers in C Programming

Pointer in C Programming language

In C language or Generally, a computer uses memory for storing instruction and values of the variables within the program, but the pointers have memory addresses as their values.

The memory address is the location where program instructions and data are stored, pointers can be used to access and manipulate data stored in the memory.In C language the computer’s memory is a sequential collection of storage cells as shown below

pointers in c programming language

In the computer’s memory, each cell is 1 byte, and it has a number called address, this address is numbered consecutively starting from zero to last address (depends upon the memory size, in 64K memory having the last address as 65,535).

While declaring a variable, the computer allocates appropriate memory to hold the value of the variable. Since every memory has a unique address, this address location will have its own address number Somewhere in the memory area.

Example: int sno=39; Variable sno

The above statement instructs the system 0Specity a location for the integer variable ‘sno’ and puts the value 39 in that location. Assume Luat the system has chosen the address location 91 for ‘sno’ this may be represented as

pointers in c language

During the execution of the program, the system always associates the variable ‘sno’ with the address 3977. We may have access to the value 39 by using either the name of the variable ‘sno’ or the address 3977.

Example Program to print address and value of the variable.

Explanation: In print() the statement ‘&‘ operator used is the address of the operator. The expression ‘&’ sno returns the ‘address of’  the variable sno (i.e.) 3977. The ‘%u’ is used for obtaining the address.

Pointers are also allotted storage location in memory, where the addresses of other variables will be stored.

pointers in c language

Pointer Concept in c language

One of the most important and powerful features in the C programming language is a pointer. It has added power and flexibility to the language.

” A pointer is a variable, it may contain the memory address of another variable”. A Pointer can have any name that is legal for other variables. It is declared in the same manner as other variables. It is always denoted by the ‘*’ operator.

Pointer features

  • Pointers are efficient in handling data and associated with arrays.
  • Pointers are used for saving memory space.
  • Pointers reduce the length and complexity of the program.
  • Use of pointer assigns the memory space and also releases it, lt helps to make better use of the available memory (dynamic memory allocation).
  • Since the pointer data manipulation is done with address, the execution time iS faster.
  • The two-dimensional and multi-dimensional array representation is easy in pointers.
  • Pointer allows for references to function, this may facilitate the passing of function as argumentS to other functions

Pointer declaration in C language

Pointer is a variable that contains the address of another variable. Like normal variables declared in ‘C’, pointers can also be declared.

Syntax: data-type   *pointer-name;

Description:

data-type  =  Specifies the type of data to which the pointer points.

Pointer-name  =  Specifies the name of the pointer.

Example:

 int    *a;
 char   *b;
 float  *c;

Accessing variables through pointers

Once the pointer is declared and assigned to the address of another variable, the variable can be accessed through its pointers.

This is done by using another unary operator * (asterisk), usually known as the indirection operator. Another name for the indirection operator is the dereferencing operator.

Example:

int *p;
x = 15;
p = &x;
VariableValueAddress
x152001
p20012005

Example: Program to access through a variable pointer.

Explanation: In the above program ‘a‘ is an integer variable and assigned values 22. The variable a is declared as a pointer variable similarly the b is a floating pe variable.

The variable *b is declared as a pointer variable. 1he statement a=&a assigns the address of a to variable a and the statement b=&b assigns the address of b to variable b. Then finally print the value of a and b.

Null Pointer in c programming language

A pointer is said to be a null pointer when its right value is 0. A null pointer can never point to a valid data. For checking a pointer, if it is assigned to 0, then it is a null pointer and is not valid.

Example: 

int *a;
int *b
b = a = 0;

Hence b and a become null pointers after the integer value of 0 is assigned to them.

Pointer to Pointer

Pointer is a variable that contains the address of another variable. Similarly, another pointer variable can store the address of this pointer variable. So we can say, this is a pointer to pointer variable.

Example: Program to illustrate pointer to pointer concept.

 

Explanation: In the above program the variable a holds the value 22, variable s is declared as a pointer.

Variable c is declared as a pointer to another pointer. once they are declared as *b and **c respectively. The address of variable a is 4gned to pointer b. Address of pointer b is assigned to C. The variable c contains areas of the pointer variable. Hence c is pointer to pointer.

The program print the valued address of variable a using variable and pointers b and c and print the address Of variable b and c.

Pointer Expressions in C language

Pointer variables can be used in expressions. Here, the pointers are preceded by the *(indirection operator) symbol.

Example:

i)    c = *a + *b;    can be written as  c = (*a) + (*b);
ii)   s = 10*-*a/*b;  can be written as  c = (10*(-(*a)))/(*b);
iii)  *p = *a**b;     can be written as  *p = (*a)**b;
iv)   *a = *a + 5;    can be written as  (*a) + = 5;

‘C’ allows us to add or subtract integers from one pointer to another pointer. In addition to that, the pointer can also be compared using relational operators. But comparisons can be used mainly in handling arrays and strings.