C Standard Library

C <stddef.h> - ptrdiff_t Type



The C <stddef.h> ptrdiff_t type is an alias of one of the fundamental signed integer type. This is a type able to represent the difference of two pointers.

ptrdiff_t is used for pointer arithmetic and array indexing. A pointer difference is only guaranteed to have a valid defined value for pointers to elements of the same array (or for past-the-last element of the same array).

In the <stddef.h> header file, it is defined as follows:

typedef /* implementation-defined */ ptrdiff_t;              

Example:

The example below shows the usage of ptrdiff_t type.

#include <stdio.h>
#include <stddef.h>
 
int main (){
  //creating an array of size 10
  int Arr[10];
    
  //pointer to the end of the array
  //(past-the-last element of the array)
  int* end = Arr + 10;

  //pointer to the start of the array
  int* start = Arr;

  //size of the array
  ptrdiff_t size = end - start;

  printf("Size of the array: %ld", size);
 
  return 0;
}

The output of the above code will be:

Size of the array: 10

❮ C <stddef.h> Library