Easy way to IT Job

Pointers in C Programming
Share on your Social Media

Pointers in C Programming

Published On: July 31, 2022

Pointers in C Programming

Pointers are variables used to store the addresses of the variable instead of respective values. The C programming language allows users to utilize pointers for dynamic memory allocation as it points to the memory location of the variable. In this article, we are going to explain pointers in C programming language along with their types.

Declaring a Pointer

Pointers can be any type of variable such as int, float, array, char, or function and the size of a pointer is based on the architecture. The pointer in C will be declared using the * asterisk symbol and it is also known as an indirection pointer that is used to dereference a pointer.

The syntax for declaring a pointer

data_type *pointer_name;

Ex: int *pt;

Pointers-C

Example

#include<stdio.h>

int main(){

int number=50;

int *p;

p=&number;

printf(“Address of p variable is %x n”,p);

printf(“Value of p variable is %d n”,*p);

return 0;

}

Output

Address of number variable is rf12

Address of p variable is rf12

Value of p variable is 45

Advantages of using pointer in C

Using pointers in C programming has the following advantages

  • Reduces the code for improving code performance
  • Used to retrieve trees, strings, and so on.
  • It returns multiple values from a function
  • It makes the user able to access any memory location

Reference and Dereference Operators

We have to use & or * operators while using pointers in C. Here is the difference between the two operators. Reference operator is also referred to as ‘Address of’ operator use & (ampersand) operator with a variable that returns us a memory location or the address of the variable. It will be used as follows

int *pt;

int a;

pt = &a

Here, ‘a’ is the address that is stored in the variable ‘pt’.

Dereference or ‘value at’ operator is defined with an * (asterisk) operator that helps the user to retrieve the value from the memory location stored in the pointer variable. Consider the following example

int *pt;

int a;

*pt = &a;

printf(The value of a = %dn”, *pt);

It displays the value that is stored in the memory location which is the value of variable a.

Types of Pointers

Numerous types of pointers will be used in C programming and the following are the popular types

Null Pointer

A pointer that doesn’t assign any value is known as Null Pointer. Users will assign a null value when they don’t have any address to specify in the pointer at the time of declaration.

Example: int *p=Null;

Wind Pointer

It will be created by not assigning any value to a pointer variable. It can be used meticulously as it provides unexpected results.

Dangling Pointer

If a pointer refers to a deleted variable or de-allocated memory, it is known as a dangling pointer as it points out a non-existing memory location.

Reading Complex Pointers in C

Reading the pointers in C can be considered as follows

Reading Complex Pointers in C
Operator Precedence Associativity
(), [] 1 Left to Right
*, identifier 2 Right to left
Data type 3

When we use (), it defines the function

When we use [], it defines the array subscript

When we use *, it defines a pointer operator

When we use the identifier, it is the name of the pointer

The data type will be used to denote the pointer that is intended to point.

C Program with Pointers

include <stdio.h>

int main()

{

int* pc, c;

c = 22;

printf(“Address of c: %pn”, &c);

printf(“Value of c: %dnn”, c); // 22

pc = &c;

printf(“Address of pointer pc: %pn”, pc);

printf(“Content of pointer pc: %dnn”, *pc); // 22

c = 11;

printf(“Address of pointer pc: %pn”, pc);

printf(“Content of pointer pc: %dnn”, *pc); // 11

*pc = 2;

printf(“Address of c: %pn”, &c);

printf(“Value of c: %dnn”, c); // 2

return 0;

}

Output

Address of c: 2686784

Value of c: 22

Address of pointer pc: 2686784

Content of pointer pc: 22

Address of pointer pc: 2686784

Content of pointer pc: 11

Address of pointer c: 2686784

Value of c: 2

Conclusion

We hope this article helps you with the basic understanding of pointers in C. Enjoy the experimental-based learning in our C C++ Training in Chennai for understanding C much better.

Share on your Social Media

Just a minute!

If you have any questions that you did not find answers for, our counsellors are here to answer them. You can get all your queries answered before deciding to join SLA and move your career forward.

We are excited to get started with you

Give us your information and we will arange for a free call (at your convenience) with one of our counsellors. You can get all your queries answered before deciding to join SLA and move your career forward.