38

In C, I have an array of structs defined like:

struct D
{
    char *a;
    char *b;
    char *c;
};

static struct D a[] = {
    {
        "1a",
        "1b",
        "1c"
    },
    {
        "2a",
        "2b",
        "2c"
    }
};

I would like to determine the number of elements in the array, but sizeof(a) returns an incorrect result: 48, not 2. Am I doing something wrong, or is sizeof simply unreliable here? If it matters I'm compiling with GCC 4.4.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Joseph Piché
  • 566
  • 1
  • 5
  • 10
  • See http://stackoverflow.com/questions/1598773/is-there-a-standard-function-in-c-that-would-return-the-length-of-an-array/1598827#1598827 for an answer that includes some hacks to make the techniques posted below safer. – Michael Burr Dec 14 '09 at 08:23

4 Answers4

49

sizeof gives you the size in bytes, not the number of elements. As Alok says, to get the number of elements, divide the size in bytes of the array by the size in bytes of one element. The correct C idiom is:

sizeof a / sizeof a[0]
Grandpa
  • 3,053
  • 1
  • 25
  • 35
48
sizeof a / sizeof a[0];

This is a compile-time constant, so you can use it to, for example, create another array:

#define N sizeof a / sizeof a[0]
int n_a[N];
Alok Singhal
  • 93,253
  • 21
  • 125
  • 158
  • In this case, if sizeof(a) is 48, sizeof(a[0]) == 24. That makes each char * take up 8 bytes. sizeof(char *) is probably either 4 or 8, depending on 32-bit/64-bit, but compilers will word align struct members, so each element ends up taking 8 bytes. – jmanning2k Dec 22 '09 at 20:24
  • 1
    Yes, but the nice thing is, the division gives the correct size whether there's padding or not, or whatever be the size of the `struct`. – Alok Singhal Dec 22 '09 at 23:34
  • #define should not have a semicolon in the end – Paamand Dec 26 '15 at 09:48
  • 1
    Note, that this might not work if "a" is declared as `extern` (although it is not the case in the original question). The compiler would not able to determine the sizeof(a) at compile time if it is declared in another source file. – elomage Jun 29 '16 at 21:17
  • 2
    The macro should be parenthesized – M.M May 11 '21 at 20:58
11

sizeof returns the size in memory of the passed element. By dividing the size of an array by a single element size, you get the elements count.

Note that the element size may include some padding bytes as well. For this reason, a padded struct (e.g. when a char member is followed by a pointer) will have a sizeof value greater than it members size sum.

On the other hand, don't let it bother you when counting elements in an array: sizeof(a) / sizeof(a[0]) will still work as smooth as expected.

ittays
  • 111
  • 3
-3

ssize_t portfoySayisi = sizeof(*portfoyler);

THIS ONE WORKS