Pointers in C

The pointers in C are the variable containing the address of the variable. This variable can be of type int, char, array, function, or any other pointer. The size of the pointer depends on the architecture. However, in 32-bit architecture, the size of a pointer is 2 byte.

A Pointer in C is used to allocate memory dynamically i.e. at run time. The pointer variable might be belonging to any of the data types such as int, float, char, double, short, etc.

Pointers are often used in C, partly because sometimes they are the only way to express calculations, and partly because they usually lead to more compact and efficient code than can otherwise be obtained.

Example Program for Pointers in C

#include <stdio.h>

int main()

{

   int *ptr, q;

   q = 70;

   /* address of q is assigned to ptr */

   ptr = &q;

   /* display q's value using ptr variable */

   printf("%d", *ptr);

   return 0;

}

Declaring a Pointer in C

The pointer in the c language can be declared with * (asterisk symbol). It is also known as an intermediate indicator used to unregister an indicator.

int    *ip;    /* pointer to an integer */
double *dp;    /* pointer to a double */
float  *fp;    /* pointer to a float */
char   *ch     /* pointer to a character */

The unary operator * is the indirection or dereferencing operator; when applied to a pointer, it accesses the object the pointer points to. Suppose that x and yare integers and ip is a pointer to into This artificial sequence shows how to declare a pointer and how to use & and *:

int x = 1, y = 2, z[10];
int *ip; 1* ip is a pointer to int *1
ip = &.x; 1* ip now points to x *1
y = *ip; 1* y is now 1 *1
*ip = 0; 1* x is now 0 *1
ip = &.z[O]; 1* ip now points to z[O] *1

Pointer to array

int arr[10];  

int *p[10]=&arr; // Variable p of type pointer is pointing to the address of an integer array arr.

Pointer to a function

void show (int);  

void(*p)(int) = &display; // Pointer p is pointing to the address of a function

Pointer to structure

struct st {  

    int i;  

    float f;  

}ref;  

struct st *p = &ref;

Pointers and Arrays

In C programming language, there is a strong relationship between pointers and arrays, strong enough that pointers and arrays should be discussed at the same time. In short any operation that can be achieved with the help of an array can also be done with pointers.

Advantage of the Pointer

  1. A pointer reduces code and improves performance, used to retrieve rows, trees, and more. and is used with arrays, structures, and functions.
  2. We can return several values from a function using the pointer.
  3. It allows access to any memory location in the computer memory.

Usage of the Pointer

There are many applications of pointers in c.

  1. Dynamic memory allocation: In C Programming Language, we can dynamically allocate memory using the malloc() and calloc() functions where the pointer is used.
  2. Arrays, functions, and structures: C pointers are widely used in arrays, functions, and structures. This lowers the code and improves performance.

Points to Remember about Pointers in C

  • The value of the null pointer is 0.
  • & symbol is used to get the address of the variable.
  • * symbol is used to get the value of the variable that the pointer is pointing to.
  • If a pointer in C is assigned to NULL, it means it is pointing to nothing.
  • Two pointers can be subtracted to know how many elements are available between these two pointers.
  • But, Pointer addition, multiplication, division are not allowed.
  • The size of any pointer is 2 byte (for the 16-bit compiler).
  • Normal variable stores the value whereas pointer variable stores the address of the variable.
  • The content of the C pointer always is a whole number i.e. address.
  • Always C pointer is initialized to null, i.e. int *p = null.

Leave a Reply

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

Shopping cart