I\'m trying to learn more about arrays since I\'m new to programming. So I was playing with different parts of code and was trying to learn about three things, sizeof(
This
void arrayprint(int inarray[])
Is equivalent to this:
void arrayprint(int *inarray)
So you are getting the size of an int pointer on your machine. Even if your question is about C++ and your function looks a bit different, it boils down to this C FAQ entry.
So, inside main
n_array
is actually a true honest array. But when passed to a function, it "decays" into a pointer. And there's no way to know the real size. All you can do is pass additional arguments to your function.
void arrayprint(int inarray[], int len)
{
for (int n_i = 0 ; n_i < len; n_i++)
/* .... */
And call it like this:
arrayprint(n_array, sizeof(n_array) / sizeof(n_array[0]));
Of course, the real solution would be to use vector
s since you're using C++. But I wouldn't know much about C++.