Pointers in c programming language with example

Programming

Pointers in c are easy and fun to learn. some c programming tasks are done more efficiently with pointers, and other tasks can not be done without using pointers, such as dynamic memory allocation. To become a complete c programmer it becomes important to learn the pointers. In clear and easy steps, let ‘s begin learning them.

What is the use of pointers in c?

A variable that stores / points the address of another variable is a pointer in the C language. The pointers in C are used to dynamically assign memory at run time, i.e. Any data type such as int, float, char, double, short, etc. can belong to the pointer variable.

Declarations of pointers

Because of a pointer ‘s ability to refer directly to the value to which it points, a pointer has different properties when pointing to a char than when pointing to an int or float. Once dereferenced, there is a need to know the type. And for that, a pointer ‘s declaration must include the type of data to which the pointer is going to point.

Syntax:
data_type *variable_name;

Example:
int *a;
char *a;

advantages of pointers in c

We’ve mentioned some advantages of using pointers below:

  • Pointers manage arrays and configurations more effectively.
  • Pointers allow function references and thus assist in transferring function arguments to other functions.
  • It also reduces the program’s length and execution time.
  • It enables dynamic memory management to be provided in c language.

Address in c programming language

Whenever a variable is specified in the c language, it will be assigned a memory location where its value will be stored. Using the & symbol, we can easily search this memory address.

If var is the variable ‘s name then & var will give the variable its address.

In order to see the memory address of any variable, we define it in our program.

pointers in c

Output:
Address of A variable: bfad8b5d
Address stored in ip variable: bfad8b5d
Value of *ip variable: 20

NULL pointers in c

Assigning a NULL value to a pointer variable is often a safe idea in the event that you do not have an exact address to assign. This is done at the declaration of the variable. A pointer with a NULL assignment is referred to as a null pointer. The NULL pointer is a constant described in many standard libraries as having a value of zero.