Thursday, 25 January 2018

What is Pointer in C language?

Welcome back! In this post, I want to tell you about Pointers in C. It used to refer to memory location of another variable without using variable identifer itself. So Let's start....

Definition:  

Pointer are widely used in programming. With pointer we can perform lot many operations by using memory addresses. It is possible to access and display the memory location of a variable using '&' operater.
A pointer variable holds the memory address of any type of variable. The pointer variable and normal variable should be of the same type. The pointer is denoted by '*' symbol.

Features of Pointers:

  • Pointer are more efficient in handling arrays and data tables.
  • Pointers can be used to return multiple values from a function via function arguments.
  • The use of pointer arrays to character strings results in saving of data storage space in memory.
  • Pointer allow C to support dynamic memory management.
  • pointer reduce length and complexity of program.
  • They increase the execution speed & thus reduce the program execution time.

Declaration of Pointer:

C programming language supports pointers to different data types such as: int, float, double, char, etc. Every pointer can point to either a single value or to multiple values. In C a pointer variable is declared the * operater.

Syntax:  int*pi;           /*pi is an integer pointer*/
              float*pf;       /*pf is a float pointer*/
              char*pc;      /*pc is a character pointer*/

For exemple, Consider the following program segment where a is an integer variable, pi is a pointer to an integer variable and &a gives the address of the integer variable a:
                    int a=10;
                     int *p;
                    p=&a;
After compliation, we have the values in the variables as shown in figure 1:

After execution of the first statement, i.e. p=&a; the values of the variables will be as shown in figure 2:

 

0 comments:

Post a Comment