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
:
- When
a
is declared as an array, like
int 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.
- When
a
is declared as an identifier for a certain object, like
int 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.
- Are there any places in C and/or C++, where an array is needed instead of just one object?
- Is there are physical difference in the size of the allocation in memory?
- Do C and C++ differ in this context?
- Is there a reason for choosing an array of one element instead of one object?
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.