0

I've got an array of arrays of TCHAR, where I store file extension.

TCHAR *extens[] = { L".*", L".txt", L".bat" };

In order to go through it, I'm calculating it's length.

int extCount = sizeof(extens) / sizeof(TCHAR);

But for some reason the extCount's value is 2. I think the problem is because this is wrong calculation method, but then, how to count the number of elements ("words") in this array correctly?

UPD: I'am passing this array to function:

void func(TCHAR *path, TCHAR **names, TCHAR **extensions);

When i'am calculating this array lenght outside function it show correct number, but inside it always workis wrong (returns 2 or 1).

UPD2: I tried to redeclare array like this:

TCHAR *extens[] = { L".txt", L".bat", L".txt", NULL };

And now inside function i'am doing something like that:

TCHAR **p = extensions;
    int extCount = 0;
    while (*p != NULL)
    {
        extCount++;
        *p++;
    }

    extCount = cnt;
    wsprintf(temp, L"%d", cnt);
    MessageBox(NULL, temp, temp, MB_OK);

It works, but looks like its not so effective, because of walking two arrays, isn't it?

Ivan Petrov
  • 155
  • 2
  • 18

3 Answers3

1

TCHAR *extens[] is an array of pointers of type TCHAR. And the size of such an array will be array_length * sizeof(pointer)).

Note: sizeof(pointer) on a system will be same for all datatypes.

paper.plane
  • 1,201
  • 10
  • 17
  • So i need to get the array length ( to know how many elements are there). – Ivan Petrov Jul 27 '15 at 08:29
  • "sizeof(pointer) on a system will be same for all datatypes". AFAIK this is not guaranteed by the C++ standard. In particular, it is possible that `sizeof(char*)>sizeof(otherdatatype*)` and TCHAR can stand for `char` (if the OP fixes the program to use _T() correctly). – n. m. could be an AI Jul 28 '15 at 12:26
1

You have an array of TCHAR*.

To get the length of the following array:

TCHAR *extens[] = { L".*", L".txt", L".bat" };

You need to use:

sizeof(extens) / sizeof(TCHAR*)
Nasser Al-Shawwa
  • 3,573
  • 17
  • 27
1

First of all, you have an array of pointers so you need

extCount = sizeof(extens) / sizeof(TCHAR*);

to calculate its size. However, this assumes that extens is still of an array-type. Once you pass it to a function expecting a TCHAR**, the array will decay to a pointer and its size information will be lost.

I think your best option would be to rewrite this in terms of std::string and std::vector. This is C++ so you might as well use its facilities. If this is not possible for any reason, and the arrays are known at compile time, you could templatize the function on array-sizes:

template <size_t N, size_t M>  
void func(TCHAR *path, TCHAR *(&names)[N], TCHAR *(&extensions)[M]);

The syntax is a bit messy maybe. For example, TCHAR *(&names)[N] is read as: "names is a reference to an array of N pointers to TCHAR". Here, the size N is deduced by the compiler as long as you don't let the array decay to a plain pointer.

JorenHeit
  • 3,877
  • 2
  • 22
  • 28
  • The problem is that arrays could be dynamically changed during execution. – Ivan Petrov Jul 27 '15 at 09:08
  • If that is the case, you can't use C style arrays at all like you do in your question. If you need dynamically sized arrays, you definately need `std::vector`. – JorenHeit Jul 27 '15 at 09:13
  • @IvanPetrov you are still using static arrays so my answer still holds. The way it is now, your arrays cannot change size. – JorenHeit Jul 27 '15 at 09:23