I am a beginner to programming and i don't know the exact meaning of sizeof(a) and sizeof(a[0]) to calculate the no of elements in an array. Why and where is this function used ? And what is the purpose of dividing them.
According to the C Standard (6.5.3.4 The sizeof and alignof operators)
2 The sizeof operator yields the size (in bytes) of its operand, which may be an expression or the parenthesized name of a type. The size is determined from the type of the operand. The result is an integer. If the type of the operand is a variable length array type, the operand is evaluated; otherwise, the operand is not evaluated and the result is an integer constant.
So if you have an array as for example
int a[N];
where N is some integer value then expression
sizeof( a )
yields the number of bytes occupied by the array. As the array has N
elements and each element in turn occupies sizeof( int )
bytes then
sizeof( a ) == N * sizeof( int )
or what is the same
sizeof( a ) == N * sizeof( a[0] )
As result you can calculate N the following way
N = sizeof( a ) / sizeof( a[0] )
It is useful if you do not know the exact size of an array for example because its size depends on the number of initializers.
For example
int a[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
const size_t N = sizeof( a ) / sizeof( *a );
Take into account that sometimes beginners make an error.
Let's assume that you have a function declaring an array as a parameter.
For example
void f( int a[10] );
and the function can be called like
int a[10];
f( a );
Beginners usually write in the body of the function the following expression
void f( int a[10] )
{
size_t n = sizeof( a ) / sizeof( a[0] );
//...
}
However it is a wrong code. The problem is that parameters declared like arrays are adjusted to pointers to the type of the array element. So the function declaration actually looks like
void f( int *a );
and within the function in expression
size_t n = sizeof( a ) / sizeof( a[0] );
parameter a
is pointer. That is it is equivalent to
size_t n = sizeof( int * ) / sizeof( int );
Depending on the used system pointers occupy either 4 or 8 bytes. So you will get either 2 or 1 if sizeof( int )
is equal to 4.
You will not get the number of elements in the array that was used as the argument.
Pointers do not keep an information about whether they point to a single object or the first object of some array.
In this case you should declare the function with second parameter that specifies the number of elements in the array. For example
void f( int *a, size_t n );
meaning of sizeof(a) and sizeof(a[0])
sizeof(a)
is the size (in bytes) of its operand, in this case, an array called a
.
sizeof(a[0])
is the size (in bytes) of its operand, in this case, a single element of the array.
... to calculate the number of elements in an array.
Divide the size of the array by the size of an element. sizeof(a)/sizeof(a[0])
Why?
For easier code maintenance. Consider using
some_type a[N];
... code that visually separates the declaration and the below loop
for (i=0; i<N; i++)
foo(a[i])
versus
some_type a[N];
... lots of code
for (i=0; i<sizeof(a)/sizeof(a[0]); i++)
foo(a[i])
With the first method, after an update of some_type a[N];
to some_type a[N+1];
, we need to search through code to also update the loop and other places that may need adjustment.
With the second method, after an update of some_type a[N];
to some_type a[N+1];
, we are done.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With