0

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 the sizeof() 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!!.

intboolstring
  • 6,891
  • 5
  • 30
  • 44
Des1gnWizard
  • 477
  • 1
  • 4
  • 13
  • 2
    "What does a+sizeof(a[0]) means here" maybe everything since it isn't used here. – MikeCAT Dec 27 '15 at 00:57
  • 1
    You can find a good explanation of the function template solution in my [answer here](http://stackoverflow.com/a/33496357/1708801) – Shafik Yaghmour Dec 27 '15 at 01:11
  • @ShafikYaghmour Thanks! – Des1gnWizard Dec 27 '15 at 01:17
  • Note that you are **much** better off rather using `std::copy(std::begin(a), std::end(a), b)`! Aside from this notation actually being short it also guards against accidental misuses of `sizeof()`: if `a` happens to be a pointer using `std::begin(a)` or `std::end(a)` will result in an error while the version using `sizeof(a)` happily compiles producing wrong results! – Dietmar Kühl Dec 27 '15 at 02:05
  • @DietmarKühl Oh thank you so much man! I will keep that in mind! I will try to use modern conventions and the prevailing methods(like using array,vector containers and string rather than using built-in array or char array). Those old-fashioned ones I will try to avoid using them! – Des1gnWizard Dec 27 '15 at 02:28

2 Answers2

3

That would simple be:

sizeof(a)/sizeof(a[0])

doesn't matter which element you chose (ie n or 0)

Paul Evans
  • 27,315
  • 3
  • 37
  • 54
  • @Des1gnWizard Expression within `sizeof` is not evaluated, you can have `*a` instead of `a[0]` as well. – LogicStuff Dec 27 '15 at 00:59
  • 1
    Yes, any element in the range (might just be to `n - 1`) will do as well as any other – Paul Evans Dec 27 '15 at 01:01
  • @PaulEvans Sure:-) But I think there's a difference, when I cout sizeof(a) it gives me the total bytes while cout sizeof(a[n]) will give me the number of elements in the array correct? – Des1gnWizard Dec 27 '15 at 01:04
  • @PaulEvans And it doesn't matter in the loop statement here because the compiler will change everything suitable for the program right? – Des1gnWizard Dec 27 '15 at 01:06
  • No, only `sizeof(a)/sizeof(a[0])` will give you the correct number of elements for *any* array `a`. Only in the case of `sizeof(a[0]) == 1` is the size correct (*ie* the size of `char a[N]` equals `sizeof(a)` – Paul Evans Dec 27 '15 at 01:09
  • @PaulEvans Sorry Paul, one last question. Normally we all represent it like a+4 or &a[0] +4 to stand for the address of the 5th element(or off-the-end), here 4 is the sizeof(a[n]), but it is also valid to use a+sizeof(a) which add bytes? So basically there is no difference adding "number of elements" and "number of bytes" here? Thank you! – Des1gnWizard Dec 27 '15 at 01:16
  • Just use plain numbers. The compiler *knows* that `a + 4` means `&a[0] + 4 * sizeof(a)/sizeof(a[0])`. The compiler is also just as happy with `*(a + 4)` as it is with `a[4]` for *any* array `a` – Paul Evans Dec 27 '15 at 01:21
  • @PaulEvans K I will just use plain number then! – Des1gnWizard Dec 27 '15 at 01:22
0

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.

Rochet2
  • 1,146
  • 1
  • 8
  • 13
  • 1
    If `a` is a pointer, then `a + sizeof(a)/sizeof(a[0])` is *not* a pointer right after the end of the array. That only works if `a` is an array. That's why it's better to use something like `std::end(a)`, which will actually give you a compile time error if you pass it a pointer. – Benjamin Lindley Dec 27 '15 at 03:04