Pointers In C Programming Language


A pointer is a variable that represents the location rather than the value of a data item, such as a variable or an array element. It is a variable that holds a memory address. This address is the location of another variable or an array element in memory.
For example, if one variable contains the address of another variable, the first variable is said to point to the second.
Similarly, A pointer is a derived data type in C. It is built from one of the fundamental data types available in C. Pointers contain memory addresses as their values. Since these memory addresses are the locations in the computer memory where program instructions and data are stored, pointers can be used to access and manipulate data stored in the memory. Pointers are undoubtedly one of the most distinct and exciting features of C language.
Read Also:
  1. Loops
  2. Functions
  3. Array
It has added power and flexibility to the language. Although they appear little confusing and difficult to understand for a beginner, they are a powerful tool and handy to use once they are mastered. Pointers are used frequently in C, as they offer a number of benefits to the programmers.
They include:
  • Pointers are more efficient in handling arrays and data tables. 
  • Pointers can be used to return multiple values from a function via function arguments. 
  • Pointers permit references to functions and thereby facilitating passing of functions as arguments to other functions. 
  • The use of pointer arrays to character strings results in saving of data storage space in memory. 
  • Pointers allow C to support dynamic memory management. 
  • Pointers provide an efficient tool for manipulating dynamic data structures such as structures, linked lists, queues, stacks and trees.
  • Pointers reduce length and complexity of programs. 
  • They increase the execution speed and thus reduce the program execution time.

Pointer Declaration:-

If a variable is going to be a pointer, it must be declared as such. A pointer declaration consists of a base type, an (*), and the variable name.
The general form for declaring a pointer variable is:

This tells the compiler three things about the variable pt_name:
  • The asterisk (*) tells that the variable pt_name is a pointer variable. 
  • pt_name needs a memory location. 
  • pt name points to a variable of type data_type. 

Programmers use the following styles:

Initialization of pointer variables:-

The process of assigning the address of a variable to a pointer variable is known as initialization of pointer variable.
Initialization is a process of assigning the address of a variable to a pointer variable. Once a pointer variable has been declared, we can use the assignment operator to initialize the variable.

We can store the address of our integer variable TOTAL in P (pointer variable). To do this we use the unary & operator.
We can also combine the initialization with the declaration. That is, 

The & operator retrieves the address of , even though TOTAL is on the right hand side. of the assignment operator =', and copies that to the contents of our pointer pir. Now, p is said to "point to" TOTAL. The only requirement here is that the variable TOTAL must be declared before the initialization takes place. We must ensure that the pointer variables always point to the corresponding type of data.
Essential Things:-
  • Passing arguments to functions by address when modification of formal arguments is to be reflected on actual arguments. 
  • Passing arrays and strings more conveniently from one function to another. Manipulating arrays more easily by moving pointers to them instead of moving the arrays themselves.
  • Creating complex data structure, such as linked lists and binary tree, where one data structure must contain references to other data structures. 
  • Communicating information about memory as in the function malloe() which returns the location of free memory by using a pointer

Post a comment