C Pointers | Pointers in C

Definition of Pointers:

A pointer is a variable which stores the address of another variable. It is called pointer because it points to a particular location in memory by storing address of that location. This variable can be of type int, char, array, function, or any other pointer.


Advantage of Pointer:

  • Pointers are more efficient in handling arrays and structures.
  • Pointers are used to return multiple values from a function.
  • We use pointers to get reference of a variable or function.
  • Pointer allows dynamic memory allocation (creation of variables at runtime) in C.
  • Pointers increases execution speed of program.
  • It makes you able to access any memory location in the computer's memory.
  • Pointer reduces the length and complexity of the program


Declaring a Pointer:

Like variables, pointers in C language have to be declared before they can be used in program. The pointer in can be declared using * (asterisk symbol). It is known as indirection operator used to dereference a pointer.


Syntax of pointer declaration:

data_type *ptr_name;


Here,

  • data_type is the pointer’s base type of C’s variable types and indicates the type of the variable that the pointer points to.
  • *(indirection operator) tells the compiler that the variable ptr_name is a pointer variable.
  • pointer_name is a valid C identifier i.e. the name of pointer variable.

 

Example:

int *a;         //pointer to integer type  

float *b;       //pointer to float type

char *c;      //pointer to character type  

 

While declaring a pointer variable, if it is not assigned to anything then it contains garbage value i.e. it may point any value in the memory.

 

Initialize a Pointer:

Pointer Initialization is the process of assigning address of a variable to a pointer variable. Pointer variable can only contain address of a variable of the same data type. In C language address operator & is used to determine the address of a variable. The address (&) operator returns the address of the variable associated with it.

int x,y;

int *ptr;       //pointer declaration

ptr=&x;       //pointer initialization

y=*ptr;        // y gets the value of x


The * operator, referred to as the indirection or dereferencing operator, returns the value of the object that its operand points to in memory. This is called dereferencing the pointer.

Pointer variable always points to variables of the same data type.


For example:

float x;

int *ptr=&x;           //ERROR, type mismatch


Accessing variable value through pointer:

After initialization pointer variable, we can access the variable’s value using the dereference operator (*). We have to use dereference operator * before the pointer variable.


For example:

int num=10;

int *ptr;

ptr=#

To access the value stored in the variable num through the pointer variable ptr, we have to use the dereference operator *

*ptr would give us the value of the variable num.


Programming Example:

#include<stdio.h>

int main()

{

int num=10;

int *ptr;

ptr=&num;

printf(“Value of num via ptr=%d,*ptr);

return 0;

}


Programming Example:

1. Program to add two numbers using pointers:


Output:



2. Program to swap two numbers using function:




Output:




    Pointer Increment and Scale Factor:


When a pointer is incremented, its value is increased by the number equal to the size of the data type that it points to. This length is called the scale factor.

If we increment an integer type pointer variable, the value of address of pointer variable will increase by 2 (size of an int). If a float type pointer is incremented then it will increment by 4 (size of a float). That is, when we increment a pointer, its value is incremented by the length of the data type that it points to. This length is called the scale factor.
                                                         

     Pointers can be incremented like,

     p1=p1+1;

     p1=p2+2;    &so on.


      The expression like p1+1 will cause the pointer p1 to point to the next value of its type. For example, If the address of p1 integer pointer is 1000 before increment, then after the operations p1=p1+1, the value of p1 will be1002, & not 1001.

 


Post a Comment

0 Comments