DS – Pointers in C

Introduction:

  • Pointers is a derived data type in C.
  • Pointer type variables store addresses of memory locations.

Pointers classified into:

  • Typed: These pointers can point to specific type data
    • int* –> points to integer data
    • float* –> points to float data
    • struct Emp* –> points to Emp data
  • Un-typed: Can points to any type of data. It is also called Generic pointer.
    • void* –> can points to any data.

Operators using with Pointers:

  • Every operation in data processing using pointers is possible through 2 operators.
  • Address Operator (&): It returns the memory address of specified variable.
  • Pointer operator (*): It returns the value in the specified address.

Following program explains the memory representation of pointer variables:

CALL BY VALUE & CALL BY REFERENCE

Call by value: A copy of the variable is passed to the function.

Call by reference: An address of the variable is passed to the function.

Note: Call by reference is preferred when we have to return more than one variable, like in C programming where we can only return one variable at a time. Call by reference can be achieved via pointers.Call by Value Example:

Call by Reference Example:

Pointers and Modify operators:

  • Modify operators increase/decrease the value of variable by 1.
  • When we apply modify operators on Pointer type variable, the address increased by size bytes and pointing to next location.
  • For example,
    • char* -> increase by 1 byte
    • int* -> increase by 2 bytes
    • float* -> increase by 4 bytes
#include<stdio.h>
int main()
{
            char c=’A’;
            float f=2.3;
           
            char* p1=&c;
            float* p2=&f;
           
            printf(“char* increment :\n”);
            printf(“p1 value : %d \n”, p1);
            printf(“++p1 value : %d \n”, ++p1);
           
            printf(“float* increment :\n”);
            printf(“p2 value : %d \n”, p2);
            printf(“++p2 value : %d \n”, ++p2);
            return 0;          
}

Pointer to array:

  • We access array locations using their index values.
  • When we specify index, the expression converts internally as
    • arr[i] -> *(arr+i)
#include<stdio.h>
int main()
{
            int arr[5] = {10,20,30,40,50};
            int i;
            printf(“Array elements are : \n”);
            for(i=0 ; i<5 ; i++){
                 printf(“%d , %d\n”, arr[i], *(arr+i));
            }
            return 0;          
}

Pointer to structure:

  • Structure stores record type information.
  • We use dot(.) operator to access structure locations.
  • When pointer pointing to structure, we use arrow(->) operator to access.
#include<stdio.h>
struct Emp
{
            int id;
            char name[20];
            float salary;
};
int main(){
            struct Emp e = {101, “amar”, 35000};
            struct Emp *p = &e;
            printf(“%d, %s, %f \n”, e.id, e.name, e.salary);
            printf(“%d, %s, %f \n”, p->id, p->name, p->salary);
            return 0;          
}
Scroll to Top