How to Find the Size of an Array in C? (original) (raw)
Last Updated : 20 Nov, 2024
The size of an array is generally considered to be the number of elements in the array (not the size of memory occupied in bytes). In this article, we will learn how to find the size of an array in C.
The simplest method to find the size of an array in C is by using sizeof operator. First determine the total size of the array in bytes and divide it by the size of one element to get the number of elements.
C `
#include <stdio.h>
int main() { int arr[] = {10, 20, 30, 40, 50};
// Total size divided by size of one element
int n = sizeof(arr) / sizeof(arr[0]);
printf("%d\n", n);
return 0;
}
`
There is also one more method is C that can be used to find the size of an array in C.
Using Pointer Arithmetic
The idea is to use **pointer arithmetic to find the difference between the memory addresses of the first element of the array and the beyond the last which will be then automatically scaled by the compiler according to the type of array.
C `
#include <stdio.h>
int main() { int arr[] = {10, 20, 30, 40, 50};
// Calculate the size
int n = *(&arr + 1) - arr;
printf("%d\n", n);
return 0;
}
`
**Note: All these methods only work as long as the array has not decayed into a pointer. Once it decays, it is treated as a pointer, and the size of the array can no longer be determined.
Similar Reads
- How to Find Size of Dynamic Array in C? In C, dynamic memory allocation allows us to manage memory resources during the execution of a program. It’s particularly useful when dealing with arrays where the size isn’t known at compile time. In this article, we will learn how to find the size of a dynamically allocated array in C. Find Size o 2 min read
- How to Find the Range of Numbers in an Array in C? The range of numbers within an array is defined as the difference between the maximum and the minimum element present in the array. In this article, we will learn how we can find the range of numbers in an array in C. Example Input:int arr[] = { 23, 12, 45, 20, 90, 89, 95, 32, 65, 19 }Output: The ra 2 min read
- How to Find the Mode of Numbers in an Array in C? In C, the mode of array numbers is the element that appears most frequently in the array. To find the mode, we can count the occurrences of each element and identify the one with the highest count. In this article, we will find the mode of numbers in C. Example:Input: myArray = { 1, 2, 3, 4, 5, 2, 3 5 min read
- How to Find the Mode of Numbers in a Sorted Array in C? The mode of the given numbers can be defined as the value that occurs the most in the given dataset or the value with the highest frequency. In this article, we will learn how to find the mode of all elements in a sorted array of integers in C. Example: Input:myArray = {1, 2, 3, 3, 5, 5, 5, 5, 6, 7} 2 min read
- How to Pass Array of Structure to a Function in C? An array of structures in C is a data structure that allows us to store multiple records of different data types in a contiguous memory location where each element of the array is a structure. In this article, we will learn how to pass an array of structures from one function to another in C. Passin 2 min read
- How to Create an Array of Structs in C? In C, a structure is a user-defined data type that can be used to group items of different types into a single entity while an array is a collection of similar data elements. In this article, we will learn how to create an array of structs in C. Creating an Array of Structs in CTo create an array of 2 min read
- How to Find Median of Numbers in an Array in C? For an odd number of elements in an array, the median is the middle element, and for an even number, it’s the average of the two middle elements. In this article, we will learn how to find median of numbers in an array in C.The median of an array can be determined is by first sorting the array and t 3 min read
- How to Find Maximum Value in an Array in C? In C, arrays are data structures that allow the user to store a collection of data of the same type. In this article, we will learn how we can find the maximum value in an array in C. Example Input: arr = {5,3,1,2,4} Output: The maximum value of the array is: 5Finding Maximum Value in an Array in CW 2 min read
- How to Use bsearch with an Array in C? The bsearch() is a library function in C that is used to perform binary search on a given sorted array. In this article, we will learn how to use search on an array in C. Example Input: arr[]= { 2, 5, 7, 11, 15 };Key=15Output:Value 15 found!bsearch() with an Array in CTo use bsearch() on a given arr 2 min read
- How to Access Array of Structure in C? In C, we can create an array whose elements are of struct type. In this article, we will learn how to access an array of structures in C. For Example, Input:myArrayOfStructs = {{'a', 10}, {'b', 20}, {'A', 9}}Output:Integer Member at index 1: 20Accessing Array of Structure Members in CWe can access t 2 min read