xxxxxxxxxx
var arr = [10,20,30,40,50]; //An Array is defined with 5 instances
var len= arr.length; //Now arr.length returns 5.Basically, len=5.
console.log(len); //gives 5
console.log(arr.length); //also gives 5
xxxxxxxxxx
#include <bits/stdc++.h>
using namespace std;
int main()
{
int arr[] = {1, 2, 3, 4, 5};
int n = sizeof(arr)/sizeof(arr[0]);
return 0;
}
xxxxxxxxxx
const xyz = ['bat', 'ball', 'mat', 'pad'];
console.log(xyz.length);
// Output: 4
xxxxxxxxxx
<html>
<head>
<title>JavaScript Array length Property</title>
</head>
<body>
<script type = "text/javascript">
var arr = new Array( 10, 20, 30 );
document.write("arr.length is : " + arr.length);
</script>
</body>
</html>
xxxxxxxxxx
// get the length of an array
// define array
let numbers = [10, 20, 30, 40, 50]
let words = ['I', 'Love', 'Javascript']
// call the .length property ( DO NOT USE () after it)
number.length // 5
words.length // 3
xxxxxxxxxx
int array[] = {4,78,3,7,9,2,56,2,76,23,6,2,1,645};
std::size_t length = sizeof(array)/sizeof(int); // see solution 1
xxxxxxxxxx
If all you have is the pointer to the first element then you can't:
int array[6]= { 1, 2, 3, 4, 5, 6 };
void main()
{
int *parray = &(array[0]);
int len=sizeof(array)/sizeof(int);
printf("Length Of Array=%d\n", len);
len = sizeof(parray);
printf("Length Of Array=%d\n", len);
getch();
}
// output: Will give two different values: 6, and 4.
xxxxxxxxxx
#include <stdio.h>
#include <stdlib.h>
void printSizeOf(int intArray[]);
void printLength(int intArray[]);
int main(int argc, char* argv[])
{
int array[] = { 0, 1, 2, 3, 4, 5, 6 };
printf("sizeof of array: %d\n", (int) sizeof(array));
printSizeOf(array);
printf("Length of array: %d\n", (int)( sizeof(array) / sizeof(array[0]) ));
printLength(array);
}
void printSizeOf(int intArray[])
{
printf("sizeof of parameter: %d\n", (int) sizeof(intArray));
}
void printLength(int intArray[])
{
printf("Length of parameter: %d\n", (int)( sizeof(intArray) / sizeof(intArray[0]) ));
}