-2

Recently, I read the code blow and it makes confused

 static string const dirs[6] = {"-n", "-ne", "-se", "-s", "-sw", "-nw" };
 int a = sizeof(dirs)/sizeof(*dirs);

And a will equals the size of the array, which is 6.

So my question is:

  1. what does sizeof(dirs) represent? Is the size of the total array?

  2. what does sizeof(*dirs) represent?

  • For at least the second, [arrays](http://stackoverflow.com/questions/4810664/how-do-i-use-arrays-in-c). – chris Jun 23 '13 at 06:14
  • 3
    What does your favorite C++ book say about what `sizeof` is and what `sizeof` does? – AnT stands with Russia Jun 23 '13 at 06:14
  • 2
    If you insist on fiddling around with arrays in c++, the preferred way of doing this is usually something like: `template size_t arraylen( T(&)[N] ) { return N; }` – Jerry Coffin Jun 23 '13 at 06:19
  • My favorite expression misuse of the day is "size of its pointer". Not even close to what that really means. (Yay, **you** should have googled this, for which you get a nice little -1 from me.) –  Jun 23 '13 at 06:27
  • @JerryCoffin: add a `constexpr` to that :P – Mooing Duck Jun 23 '13 at 06:30

5 Answers5

2
  1. sizeof(dirs) represents the size of the entire array
  2. sizeof(*dirs) represents the size of a single element of the array

Thus, sizeof(*dirs) * number of elements = sizeof(dirs), because number of elements * size of each element = size of entire array

Thus, the number of elements = sizeof(dirs) / sizeof(*dirs).

jh314
  • 27,144
  • 16
  • 62
  • 82
  • 1
    "`sizeof(*dirs)` represents the size of a single element of the array" - more precisely, that of the first one. –  Jun 23 '13 at 06:28
2
what does sizeof(dirs) represent? Is the size of the total array?

Yes, it is.

what does sizeof(*dirs) represent?

*dirs is same with dirs[0], so sizeof(*dirs) is the size of the first element. (well ,the size of every element because they are the same)

sizeof(dirs)/sizeof(*dirs) will be the number of elements in the array.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
0

sizeof operator yields the size in bytes of the provided operand. Since *dir is equal to dir[0] the sizeof(*dirs) will return the size of the first array element in bytes wile sizeof(dirs) will return the size in bytes of all array. Hence when you divide those numbers you get the number of elements in array.

More on sizeof operator: http://en.wikipedia.org/wiki/Sizeof http://en.cppreference.com/w/cpp/language/sizeof and http://msdn.microsoft.com/en-us/library/4s7x1k91(v=vs.110).aspx

Robertas
  • 1,164
  • 3
  • 11
  • 26
0

When the sizeof operator is applied to a reference, the result is the same as if sizeof had been applied to the object itself.

If an unsized array is the last element of a structure, the sizeof operator returns the size of the structure without the array.

The sizeof operator is often used to calculate the number of elements in an array using an expression of the form:

sizeof array / sizeof array[0]
BlackMamba
  • 10,054
  • 7
  • 44
  • 67
0

One important exception to the above answers is that if dirs is passed to a funciton, sizeof (*dirs) will still be the size of one element in the array, but sizeof dirs will now evaluate to the size of a pointer type in your environment. This is because the array passed into a function is just a pointer when used inside the function.

gled
  • 121
  • 4