What is Pointer in C ?

  • For storing the address of another variable pointers can be used.
  • Variables of type int,char, array, function or any other pointer can be of type pointer.
  • Architecture of a pointer determines the size of the pointer.
  • Size of the pointer depends on the architecture.
  • In 32 bit the size of the pointer is 2 byte.
  • Using pointer some of programming tasks in C can be easily done.

Declaring a pointer

The pointer in c language can be declared using * (asterisk symbol). It is also known as indirection pointer used to dereference a pointer.

[pastacode lang=”c” manual=”int%20*a%3B%20%2F%2Fpointer%20to%20int%20%20%0Achar%20*c%3B%2F%2Fpointer%20to%20char%20%20%0A” message=”” highlight=”” provider=”manual”/]

Pointer Example:

An example of using pointers to print the address and value is given below.

Pointer to array

[pastacode lang=”c” manual=”int%20arr%5B10%5D%3B%C2%A0%C2%A0%0Aint%20*p%5B10%5D%3D%26arr%3B%C2%A0″ message=”” highlight=”” provider=”manual”/]

Pointer to a function

[pastacode lang=”c” manual=”void%20show%C2%A0(int)%3B%C2%A0%C2%A0%0Avoid(*p)(int)%C2%A0%3D%C2%A0%26display%3B%C2%A0″ message=”” highlight=”” provider=”manual”/]

 Pointer to Structure

[pastacode lang=”c” manual=”struct%C2%A0st%C2%A0%7B%C2%A0%C2%A0%0A%0A%C2%A0%C2%A0%C2%A0int%C2%A0i%3B%C2%A0%C2%A0%0A%0A%C2%A0%C2%A0%C2%A0%C2%A0float%C2%A0f%3B%C2%A0%C2%A0%0A%0A%7Dref%3B%C2%A0%C2%A0%0A%0Astruct%C2%A0st%C2%A0*p%C2%A0%3D%C2%A0%26ref%3B%C2%A0%C2%A0″ message=”” highlight=”” provider=”manual”/]

Advantages of Pointer

  • Pointer reduces the code and improves the performance.
  • It is used to retrieving strings, trees, etc., and used with arrays, structures, and functions.
  • We can return multiple values from a function using the pointer.
  • It makes you able to access any memory location in the computer’s memory.

Usage of pointer

There are many applications of pointers in c language.

Dynamic memory allocation

  • In C language, we can dynamically allocate memory using malloc() and calloc() functions where the pointer is used.

Arrays, Functions, and Structures

  • Pointers in c language are widely used in arrays, functions, and structures.
  • It reduces the code and improves the performance.

Leave a Reply

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

You May Also Like