DWORD Array Length

I've tried Google, though I couldn't find any that worked. sizeof don't work here, so how would I find the length?

Array: DWORD offsets[] = {0x378, 0x14, 0x0};
Tried: int levels = sizeof(offsets);
And: int levels = sizeof(offsets) / sizeof(*offsets);

I've also tried using a while loop (since that's what this is for in the end, a for loop), and I couldn't get it to work.

Tried: int i = 0; while (offsets[i] != NULL) {}

I'm pretty sure the zero value isn't working, because another array passed through it (with only one item) worked. So, it counted the one.

I'm really new to C++, so please don't think I'm just dumb or not trying.
int levels = sizeof(offsets) / sizeof(*offsets);
This is how you claculate the number of elements of an array. What's wrong with it?
sizeof(offsets)/sizeof(DWORD);
actually i believe a DWORD is 32 bits
@coder777 Not sure. That's why I'm asking.

@gelatine That didn't work either.

I'm wanting the answer to be 3, which works fine when I do int levels = 3; but the size of the DWORD array changes, so I can't keep it like that.

I can use another parameter in the function and manually count them, but I would just like to understand what I'm doing wrong. With that solution though, the single element array worked again, but the three element array didn't.

/e If it helps, gelatine's method returned a value of 1.
Last edited on
sizeof(Offsets) = 4
sizeof(*Offsets) = 4
sizeof(DWORD) = 4

So, when it divides any of them, the results comes out to 1. The arrays (1 and 3 element arrays) both come out to the same values.
I can suspect that you are trying to determine a size of an array that was passed to a function. Arrays are implicitly converted to a pointer to their first element when they are passed to a function as argument. For example

1
2
3
4
5
6
7
8
void f( int a[] )
{
   size_t size = sizeof( a ) / sizeof( *a ); // size is equal to 1
}

int a[] = { 1, 2, 3 }; // the size of the arrray is equal to 3

f( a );



Inside f a is a pointer to the first element of an array passed to the function. So you will get 1 if sizeof( int * ) is equal to sizeof( int ).
Last edited on
So, there's no way to do it inside the function? I tested and did ..

sizeof(Offsets) / sizeof(*Offsets)

.. as a parameter and it worked, but it would be nicer to get it inside the function so it does it itself.

If it ain't possible, I can just call sizeof for each one and pass it through, it's not a real big deal. Just wondering if possible though.
Alright. Thanks guys for helpin'. (:
You can if you will pass an array by reference.

For example

1
2
3
4
5
6
7
8
void f( DWORD ( &a )[3] )
{
   size_t = sizeof( a ) / sizeof( *a ); // size is equal to 3 
}

DWORD offsets[] = {0x378, 0x14, 0x0};

f( offsets );
Topic archived. No new replies allowed.