Reputation: 497
To avoid things quietly breaking if you change the array size, I suggest
std::copy(a, a + sizeof(a)/sizeof(a[0]), b);
. Even better, wrap thesizeof()
junk in a macro -- or even betterer, use a function template instead:template <typename T, size_t N> size_t size((T&)[N]) { return N; }
– j_random_hacker Sep 8 '12 at 7:29
When i I was looking into Q&A this morning I found this comment(with 4 upvotes). I'm quite new at C++. What does a+sizeof(a[0])
means here, I thought sizeof(a[0])
will return 4 which stands for a int memory byte? Many thanks in advance!!.
Upvotes: 0
Views: 123
Reputation: 1156
If a
is an array then sizeof(a)/sizeof(a[0])
is the amount of elements in a
. It is the size of whole array per size of one element.
So if a
is a an array, then a + sizeof(a)/sizeof(a[0])
is a pointer to right after the end of the array.
Upvotes: 0
Reputation: 27577
That would simple be:
sizeof(a)/sizeof(a[0])
doesn't matter which element you chose (ie n
or 0
)
Upvotes: 3