How to find an array size without using sizeof() and through Pointers Subtraction

In this tutorial, you will learn to find an array size without using sizeof() function and using a subtraction between using two pointers. In the last tutorial, we have seen that subtraction between two pointers provides a number of elements between an array, and in other words, it is used to define the how far the elements from each other in memory. But, subtraction of two pointers can also be used to find an array length of any data type without using computationally expensive sizeof() library function.

Function of Array name as Pointer

For example, we have intitialzed this array three integers and we want to find the lenght of this array using pointer and without using sizeof() function.

Find an array length without using sizeof function and using pointer example in c

First keep in mind the following points:

Using &x gives you the address of the first element of the array and &x is a pointer to itself. In other words, &x is a pointer to an array(itself ) off size 3 i.e. int *[3].

Because &x is pointer to an of size of 3. Hence, if we add constant number 1 to &x, the number 12 will be added to its address and it will start pointing to a byte or memory right after the array. Hence, &x+1 points to an memory location right after the array.

Find an array length without using pointer arithmetic

*(&x + 1) provides the address of the byte which is available right after the array x that is 216 according to the above figure. Remember, addresses used in above picture are just for demonstration and memory assigned to each variable during run-time of a program and each fresh run of the program will get different memory space. *(&x + 1) is basically dereferencing pointer to an array of 3 elements and it yields the address of the first byte. Because pointers always point to the base address of elements to which it points to.

Now *(&x + 1) is a pointer to an array of three elements and it points to the next element of array x and &x is also a pointer to itself with 3 elements. Because both these expression are pointers of the same type and we subtract these pointers from each other like this:

*(&x + 1) - &x;  // 204 - 216 = 12/4 = 3 elements

Note: Subtraction of two Pointer variables of the same data type provides the number of elements between them.

C Programming Code

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

void main(void) {
	int x[] = { 10, 15, 20};
	int i = *(&x+ 1) - (&x); // difference in pointers 
	printf("Lenght of an array x[] = %d\n", i); // 0 output
	}

Output:

Lenght of an array x[] = 3

We can use the same code to find lenth of string or array of any other data type. For example, the code given below prints the lenght of string using the above given same code.

#include<stdio.h>
#include<stdlib.h>
void main(void) {
	char x[] = "ABC";
	int i = *(&x+ 1) - (&x); // difference in pointers 
	printf("Lenght of string x[] = %d\n", i); // 0 output
	}

output:

One important point to note here is that output shows that the string lenght is 4. But, in initialization, it contains three character elements only. This is becuase whenever, we intitialize a string, a null character is implicitly added at the end of the string.

Lenght of string x[] = 4

Leave a Reply