I wonder about if there is a reason to use an array, consisted of one element object, instead of just to use one object.
Like, f.e.
int a[1] = {10};
instead of just
int a = 10;
Of course, there is a big difference in the context of accessing the value of 10
in the later code between both cases, by the use of a
:
a
is declared as an array, likeint a[1] = {10};
a
decays to a pointer to the first element object of the array of a
, thus a
evaluates to type int*
, when a
is used later in the program.
a
is declared as an identifier for a certain object, likeint a = 10;
a
is of type int
.
If we would want to pass the value of 10
to a function, we have to take attention on that and must distinguish between both uses.
But that does not answer my question for why I use the one way instead of the other one.
Reason for tagging with C and C++: I tagged it with both language tags because both statements are valid in C and C++, and at the actual moment, I don´t know of any difference between both languages in that case. If there is a difference, please mind it.
Thanks for your help.
Are there any places in C and/or C++, where an array is needed instead of just one object?
I think you mostly will encounter a C-style array with one object as the end-case of a recursive function on one object. If you have a function that takes an array in a C-like manner (f(int* array, int length)
) you could use this to apply the function to only one input without providing another overload. But you could do the same with int a = 42; f(&a, 1);
.
Is there are physical difference in the size of the allocation in memory?
No. int a[1] = {42}
and int a = 42
do exactly the same to the stack.
Do C and C++ differ in this context?
No.
Why do I use an array of one element instead of just one object?
An array with compile-time size of 1 (like you did above) is probably used very little. There is a difference between
int a[1] = {42};
// and
int b = 42;
int *a = &b;
since an array is not a pointer but only decays to one. But the difference in using them is very minor. The most notable difference in using them (thx @Scheff) should be that you can use std::begin(a)
and std::end(a)
on int a[1]
but not on int *a
. This is only a difference in C++. Note that this disappears once the array has decayed to a pointer. Then you substitute them by a
and a+1
respectively.
I could imagine, that one do int a[1] = {42};
, if one wants to initialize an object on the stack and directly have a pointer to it (and not really use the value itself), since it is a bit shorter. But I have never seen that myself and I would actually consider it bad style. I would think that it would be very uncommon in C++ code, since raw-pointers themselves are not that common.
I have actually more often seen an array of length 0, see for usage here. Since those seem to be technically illegal in older versions of C are invalid in C (thx for the comments), one could use an array of length 1 to be legal. But to the best of my knowledge, most compilers implemented 0 length arrays since pretty much forever, so most people would probably ignore that and use a 0 length array.