Internship at OpenGenus

Become this book -> Problems on Assortment: For Interviews and Competitive Programming

Reading time: 20 minutes | Coding time: 5 minutes

Unlike other languages where array is defined by the starting retentivity accost, datatype and the length of the array, in C, array is a similar pointer to a retentivity location which is the starting memory address. We need to utilize the sizeof operator in C/ C++ to attain this.

The fundamental idea of getting the length of an assortment in C or C++ is:

            int length = sizeof(array)/sizeof(<data_type of the array>);  // length = total size of array / size of an array element                      

For Integer assortment, we can observe the length as follows:

            int length = sizeof(array)/sizeof(int);                      

This works with array of user defined objects (in C++) and struct (in C and C++) as well. We will demonstrate this on the following examples:

  • Assortment of Integer
  • Array of Character
  • Assortment of Struct
  • Assortment of user divers object (in C++)

We volition go through each instance in particular.

Array of Integer

In this instance, we volition empathise how to get the length of an array of integers. Nosotros defined an array of integers similar this:

            int assortment[] = {one, 3, 2};                      

Following this, we found the length of the array every bit follows:

            int length = sizeof(array) / sizeof(int);                      

Following is the complete C example:

            // Role of OpenGenus IQ #include<stdio.h> int main()  {     // define sample array	 	int array[] = {i, 3, two}; 	// get length of array as explained by OpenGenus 	int length = sizeof(array) / sizeof(int); 	// print length of array 	printf("Length of assortment is %d\n", length); 	return 0; }                      

Output:

            Length of assortment is 3                      

Array of Character

In this example, we will sympathize how to become the length of an array of characters. We defined an array of characters like this:

            char assortment[] = {'o', 'p', 'east', 'n', 'grand', 'e', 'n', 'u', 's'};                      

Following this, we found the length of the assortment as follows:

            int length = sizeof(array) / sizeof(char);                      

Following is the complete C example:

            // Part of OpenGenus IQ #include<stdio.h> int main()  {     // define sample assortment	 	char array[] = {'o', 'p', 'due east', 'n', 'thou', 'east', 'n', 'u', 'south'}; 	// get length of array every bit explained by OpenGenus 	int length = sizeof(array) / sizeof(char); 	// impress length of array 	printf("Length of array is %d\n", length); 	return 0; }                      

Output:

            Length of assortment is 9                      

Array of struct

In this case, we will empathize how to get the length of an array of struct. We divers a struct like this:

            struct opengenus {         int data;      bladder weigth; };                      

We used the above struct to go an array of struct:

            struct opengenus array[5];                      

Following this, we found the length of the array as follows:

            int length = sizeof(assortment) / sizeof(struct opengenus);                      

Post-obit is the complete C example:

            // Part of OpenGenus IQ #include<stdio.h> //define a struct struct opengenus {         int information;      float weigth; };    int main()  {     // define sample assortment	 	struct opengenus assortment[five];  	// fill in the array 	for(int i = four; i>=0; i--) 	{ 	    assortment[i].data = i; 	    array[i].weigth = i*2+0.1; 	} 	// become length of array equally explained by OpenGenus 	int length = sizeof(array) / sizeof(struct opengenus); 	// print length of array 	printf("Length of assortment is %d\n", length); 	return 0; }                      

Output:

            Length of array is v                      

Assortment of User divers object in C++

In this example, we will sympathize how to go the length of an array of user divers objects. We defined a user defined grade like this:

            class opengenus  {    public:       double data;          double weight;  };                      

Nosotros used the above class to get an array of user divers objects:

            opengenus array[6];                      

Following this, we plant the length of the array equally follows:

            int length = sizeof(array) / sizeof(opengenus);                      

Following is the complete C instance:

            // Office of OpenGenus IQ #include <iostream> using namespace std; //define a class course opengenus  {    public:       double data;          double weight;  }; int main()  {     // define sample array	 	opengenus array[6];  	// fill in the array 	for(int i = 5; i>=0; i--) 	{ 	    assortment[i].data = i; 	    array[i].weight = i*2+0.i; 	} 	// get length of array every bit explained by OpenGenus 	int length = sizeof(array) / sizeof(opengenus); 	// impress length of assortment 	cout << "Length of array is " << length; 	return 0; }                      

Output:

            Length of array is 6                      

With this, you have the complete idea of getting the length of any array in C and C++ using sizeof operator. Savor.