Pointers in C Programming with examples

Pointers in c programming language; In this tutorial, i am going to show you about pointers in c programming with the help of it’s syntax, declaration, types of pointers, advantages, disadvantages and examples.

Pointers in C Programming

Here, i will show you about pointers in c programming with the help of it’s syntax, declaration, types of pointers, advantages, disadvantages and examples:

  • What is Pointers
  • Syntax of Pointers
  • Declaring a pointers
  • Types of Pointers
  • Advantage Of Pointers
  • DisAdvantage Of Pointers
  • Example of Pointers in C

What is Pointers

In c programming, A pointers are special variables that stores the addresses of another variables. While other variables store the value of a variable of a specified type (int, char, float, etc.), the same pointer stores the address of a variable. And every pointer variable has a unique address in memory. And these addresses are in hexadecimal form.

For example, an float variable stores an float value, however an float pointer holds the address of a float variable.

Syntax of Pointers

The syntax of pointer in c; as shown below:

data_type *var_name; 

Declaring a pointers

You can declare pointers in c using the following way:

int* p;

Where, * is used to denote that “p” is pointer variable and not a normal variable.

And, also, you can declare pointers in the following ways:

int *p1;
int * p2;

Note that:- The * Operator is also known as Value at address operator.

Types of Pointers

There are eight different types of pointers they are:

  • Null pointer
  • Void pointer
  • Wild pointer
  • Dangling pointer
  • Complex pointer
  • Near pointer
  • Far pointer
  • Huge pointer

Advantages of Pointers

  • Less time in program execution
  • Working on the original variable
  • With the help of pointers, we can create data structures (linked-list, stack, queue).
  • Returning more than one values from functions
  • Searching and sorting large data very easily
  • Dynamically memory allocation

Disadvantages of Pointers

  • Sometimes by creating pointers, such errors come in the program, which is very difficult to diagnose.
  • Sometimes pointer leaks in memory are also created.
  • If extra memory is not found then a program crash can also occur.

Example of Pointers in C

#include <stdio.h>
int main()
{
 int num = 10;
 printf("Value of variable num is: %d", num);
 /* To print the address of a variable we use %p
 * format specifier and ampersand (&) sign just
 * before the variable name like &num.
 */
 printf("\nAddress of variable num is: %p", &num);
 return 0;
}

Output of the above c pointer program is:

Value of variable num is: 10
Address of variable num is: 0x7fff5694dc58

Be First to Comment

Leave a Reply

Your email address will not be published.