C Pointers

In this tutorial, we are going to learn pointers, what is pointer, when to use pointers, usage of pointers, pointer that point to a variable, pointer that points to an array or a structure, pointer in UDF etc.,

What is Pointer?

Pointer is variable that points to another variable. Lets assume that we have a variable age in our program, &age points to the address of that variable.

To scan a above age variable, we write like this scanf("%d",&age). Address operator & is use to refer an address in C language.

Benefits of Pointer

  • Pointers are more efficient in handling arrays and data tables.
  • Pointers can be used to return multiple values.
  • Use of Pointer arrays to characters strings is helpful in saving memory.
  • It is use to allocate dynamic memory (runtime).
  • Pointer increases the execution speed of a program.

Lets see how to get an address of a variable

#include <stdio.h>

// www.raviroza.com
// 9-Dec-2020, 7.57 pm

void main()
{
  int age = 5;
  printf("Value of age is : %d\n\n", age);

  // get the address of age variable
  printf("Address of age is : %p", &age);  
  getch();
}

How to declare Pointer?

Syntax:

dataType *variableName;

Code snippet

int age = 100;

int *ptr;

ptr = &age;

// to alter/change the value of age by using *ptr;

*ptr = *ptr + 1;

Here, age is an integer variable, and *ptr is pointer variable that is referencing or pointing to the variable age.

Interestingly, it is possible to change the value of age variable by using *ptr as it is done in the last line of code.

Guidelines to declare pointers

  • Pointer variables ares declaring using the asterisk (*).
  • Pointer itself needs a memory location.
  • Memory Variable to refer and the pointer variable should of same type, means that a int pointer can refer only a int memory variable.

Example – Pointer of int, float and char data type

# include <stdio.h>
# include <conio.h>

// www.raviroza.com
// 11-Dec-2021, 8.00 am

void main()
{
    printf("\n Pointer Demo \n");

    int i = 10;
    int *iptr;
    
    float f = 10.34;
    float *fptr;
    
    char c = 'r';
    char *cptr;
    
    iptr = &i;
    fptr = &f;
    cptr = &c;
    
    printf ("int   : %d is at %p\n",i,iptr);
    printf ("float : %f is at %p\n",f,fptr);
    printf ("char  : %c is at %p\n",c,cptr);
    
    getch();
}

Pointer and Array

The array is also known group of sequential data in memory. Let’s see with an example to get the address of each array element

Example 1 : Address of Array elements

# include <stdio.h>
# include <conio.h>

// www.raviroza.com
// 11-Dec-2021, 9.00 am

void main() 
{
   int arr[5];
   int i;

   printf ("\n\n POINTERS/ADDRESS OF ARRAY ELEMENTS \n");

   for(i = 0; i < 5; ++i)
   {
      printf("&arr[%d] = %p\n", i, &arr[i]);
   }

   printf("Base Address of Array is : %p", arr);

   getch();
}

Example 2 : Pointer to an Array

# include <stdio.h>
# include <conio.h>

// www.raviroza.com
// 11-Dec-2021, 9.00 am

void main() 
{
     int arr[5] = {1, 2, 3, 4, 5};
     int* ptr;

     // ptr is assigned the address of the 1st element
     ptr = &arr[0]; 

     printf("*ptr     : %d\n", *ptr);      // 1
     printf("*(ptr+1) : %d\n", *(ptr+1));  // 2
     printf("*(ptr+2) : %d",   *(ptr+2));  // 3
     getch();
}

Example 3 : Array of Pointers

# include <stdio.h>
# include <conio.h>

// www.raviroza.com
// 08-Oct-2022, 12.00 pm

void main() 
{
    
    int x=100, y=200;
    // array of integers
    int arr[5] = {1, 2, 3};
     
    // array of integer pointers
    int *ptr[5];
         
    int i;
     
    // assign address of each array elements to pointers
    ptr[0] = &arr[0];
    ptr[1] = &arr[1];
    ptr[2] = &arr[2];
    // assign integer x and y to pointers
    ptr[3] = &x;
    ptr[4] = &y;

    for(i=0; i<5; i++)
    {
        // modify the value of variable x, y and array elements using array of pointers
        *ptr[i] = *ptr[i] + 2;
        printf ("%d  ",*ptr[i]);
    }
    getch();
}

Pointers and Structure

Example 1 : Pointer to Structure

# include <stdio.h>
# include <conio.h>

// www.raviroza.com
// 7-Oct-2022, 8.25 am

struct student
{
   int age;
   float weight;
};

void main()
{
    struct student *studentPtr, student1;
    studentPtr = &student1;   
    clrscr();

    printf("Enter age : ");
    scanf("%d", &studentPtr->age);

    printf("Enter weight : ");
    scanf("%f", &studentPtr->weight);

    printf("Displaying Student Details:\n");
    printf("Age: %d\n", studentPtr->age);
    printf("weight: %f", studentPtr->weight);

    getch();
}

Example 2 : Dynamic memory allocation of structs

# include <stdio.h>
# include <conio.h>

// www.raviroza.com
// 7-Oct-2022, 8.25 am

#include <stdio.h>
#include <stdlib.h>

struct student 
{
   int age;
   char name[30];
};

void main()
{
   struct student *ptr;
   int i, n;

   printf("Enter the number of students: ");
   scanf("%d", &n);

   // allocating memory for N numbers of struct student
   ptr = (struct student*) malloc(n * sizeof(struct student));

   for(i = 0; i < n; ++i)
   {
       printf("Enter first name and age respectively: ");

       // To access members of 1st struct student,
       // ptr->name and ptr->age is used

       // To access members of 2nd struct student,
       // (ptr+1)->name and (ptr+1)->age is used
       scanf("%s %d", (ptr+i)->name, &(ptr+i)->age);
   }

   printf("Displaying Student Information:\n");
   for(i = 0; i < n; ++i)

   {
       printf("Name: %s\tAge: %d\n", (ptr+i)->name, (ptr+i)->age);

   }
   getch();
}

Pointer and Function

When a pointer is passed as an argument to a function, it is also known as function with reference parameter. When a function called by reference is, the address (pointer or address) of a variable is passed to the function. Following is an example of pointer as an argument to a function

# include <stdio.h>
# include <conio.h>

// www.raviroza.com
// 8-Dec-2021, 10.30 am

void byref(int*);

void byref(int *i)
{
	*i = *i + 1;
}

void main()
{
	int temp=10;
	
	byref(&temp);
	printf ("\n\ntemp = %d",temp);
	getch();
}
%d bloggers like this: