C Pointers

The significance of pointers in C is the flexibility it offers in the programming. Pointers enable us to achieve parameter passing by reference, deal concisely and effectively either arrays, represent complex data structures, and work with dynamically allocated memory.

Although a lot of programming can be done without the use of pointers, their usage enhances the capability of the language to manipulate data. Pointers are also used for accessing array elements, passing arrays and strings to functions, creating data structures such as linked lists, trees, graphs, and so on.

Only the C Language supports pointers and they play an important role in it. Pointers are very simple to use, provided the basic concept is understood properly. Mainly pointers are used in C Program to acquire as much storage space to store more values at a time and at the same time allow the program to deallocate the storage, which is no more required in the program. Learning of pointer may be difficult, but not too difficult. It looks difficult because it is a new topic and pointers are not used in the basic learning languages like Basic, Cobol, Fortran, etc.

Definition

Pointers are memory addresses variable i.e the variables, which have addresses of the variable having some value, stored in the memory. Further, we can say pointers are directly linked to the memory address. Actually, pointer is a variable having an address of another variable and not the value. For example, suppose q is an integer variable and this variable has a value of 250 stored in the memory.

It is represented as follows:

Variable name Memory value Memory address Memory variable
q 250 5000 p (pointer)

Advantages of Pointer

Pointers are used to direct link the memory address. Now question arises, Why pointers are required. To solve this question some reasons for the use of the pointer are discussed as follows:

  • A pointer enables the varible, which is used outside the function or used in the another Sub-program.
  • Pointers increase the execution speed of the C-Program and are more efficient.
  • Pointers reduces the length and complexity of the program.
  • Pointers accesses the memory elements very easily.
  • Pointers are more efficient in handling the date table i.e. two-dimensional way.
  • Use of the pointer to the character array or to the string save the storage space in the memory.
  • Pointers have direct link with structure and union.
  • By using pointer, we can declare lesser number of variables in memory.
  • Pointers help to return more than one value from the function sub-program.
  • By using pointers, function arguments can be passed to the functions.
  • Pointers saves the memory space. This can be done by using the dynamic memory allocation technique.
  • Pointers are also very useful to handle files.
  • Pointers are very useful in data structure manipulation.

Declaring a Pointer Variable (Initialization of Pointer)

Declaration of pointer variable is similar to the declaration of a common variable. A Pointer variable should be declared before they are used. When we want to use pointer variable, then these should be declared in data-type statement used in the beginning of main program. The general syntax used for the declaration of pointer (memory variable) is as:

data-type *pointer-variable;

Where data-type may be integer (int), real (float), character (char) or double. Also here * (asteric sign) means it is pointer operator and pointer variable is any variable linked with ‘*’ sign . The symbol * is called indirection operator. Some valid pointer declaration statement are as follows:

int *p;
float *y;
char *c;

For example, suppose if p is a pointer variable, which gives the address of the variable, then we can use the above statement as:

int *p, q=250;
p = &q; /* This is initialization of Pointer*/printf("\n %d is stored at address %u",q,p);

Here the result be as follows:

250 is stored at address 5000
Note: The format code in the control string of printf() statement should be %u because addresses are always unsigned integer.

Pointer Variables

Example Program:

#include <stdio.h>
#include <conio.h>
void main()
{
  int a,b;
  int *p,**t;
  a = 10;
  b = 30;
  p = & a;

  printf("\a = %d",a);     /* 10 */  printf("\a = %u",&a);     /* 4080 */  printf("\a = %d",*(&a))  ;   /* 10 */  printf("\P = %u",p)  ;   /* 4080 */  printf("\P = %u",&p)  ;   /* 8080 */  printf("\P = %d",*p)  ;   /* 10 */  printf("\P = %d",*(&p))  ;   /* 10 */  t = & p;
  printf("\T = %u",t)  ;   /* 8080 */  printf("\T = %u,&t)  ;   /* 2040 */  printf("\T = %u",*(&t))  ;   /* 4080 */  printf("\T = %d",**(&t))  ;   /* 10 */  printf("\T = %d",**t)  ;   /* 10 */
  getch();
}

Pointer Program Using Function (Pass By Reference)

Example Program:

/*Pointer Program Using Function (Pass By Reference) */
#include <stdio.h>
#include <conio.h>
void main()
{
  int a,b;
  void interchange(int *, int *);
  clrscr();
  printf("Enter the Two No.");
  scanf("%d%d",&a,&b);
  interchange(a,b);
  getch();
}

void interchange(int *x, int *y)
{
  int t;
  t = *x;
  *x = *y;
  *y = t;

  printf("A=%d B=%d", *x,*y);
}
Related Post