Pointers in C Language

The most important topic in C-language is Pointers. But what are pointers in C language, how to declare pointers? , types of pointers, uses of pointers, and programming using pointers in C language. In this tutorial, we shall cover all the concepts about the pointers that answer the above questions.

Definition

Pointer is a variable that stores the address of another variable. Pointer is mainly used to allocate the memory dynamically (at run time).

Key Points

  • Normal variable stores a value but Pointers in C language stores the address of that value 
  • Pointer variables might belong to any of the data type such as int,float,char etc…
  • The value of null pointer in 0

            int *p=null;

  • It is used to access the information of a particular memory location
  • A pointer can also be used to refer to another pointer.
  • A pointer can also be incremented/decremented to next/previous memory location.
  • The main advantage of a pointer is to decrease the memory space and lessen the execution time.

Syntax

      data_type *identifier;

                   or

      data_type* identifier;

Declaration and Example

      int *a;  or  int* a;

     Here we are telling the compiler that ‘ a ‘ is a pointer where we can store an address.

     a=&p;

     Here we are assigning the the address the pointer variable ‘ p ‘ to the variable ‘ a ‘.

Description of the above example

Here, ‘int’ is the data type (integer) and ‘a’ is the name of the pointer, we have specified. The asterisk sign is mainly used for pointer specification. We can use both the types of syntax for declaration of a pointer. 

Types of pointers

 1.Typed pointer 

    It only points to a specific type of data.

     Ex  –  int* – int type of data

              double* – double type of data

              float* – float type of data

 2.untyped pointer

    It points to any type of data by using ‘void’ keyword.

    Ex  –  void* – any type of data

Types of pointers(based on operation)

1. Address operator

  • This operator returns the address of a particular variable.
  • The symbol for address operator is ‘&’.

2. Pointer operator

  • This operator returns the value in the specified address.
  • The symbol for the pointer operator is ‘*’.

Example program

Let us write an example program for execution and understanding of operators concept.

void main()
{
    int i=100;
    int *ptr;
    ptr=&i;
    printf("%d",i);
    printf("%d",ptr);
    printf("%d",&i);
    printf("%d",&ptr);
    printf("%d",*ptr);
    printf("%d",*(&i));
}

Output

100

40657

40657

67543

100

100

Description of output

Here, i value is 100 and ‘ &i ‘ is the address of i. And ‘ ptr ‘ means ‘ &i ‘ .So ‘ &i ‘ = ‘ ptr ‘ = 40657. 67543 is the address of ptr i.e.’ &ptr ‘ = 67543.

Again ‘ *ptr ‘ means the value stored in the specified address which is ‘ ptr ‘ .So ‘ *ptr ‘=100’ *(&i) ‘ means the value stored in the address of i, which is 100. 

Uses of pointers

  • Through pointers, we can be able to know the address of data.
  • We can be able to retrieve the data again through pointers if the data is lost.

Conclusion

This is all about the basic concepts of the pointer and you are covered perfectly. For any sort of doubts or questions, regarding this article, you can reach out through the comment box.

Also learn:
Web development

Spread knowledge

1 thought on “Pointers in C Language”

Leave a Comment

Your email address will not be published. Required fields are marked *