2

In my code I have an array of wchar_t:

wchar_t paths [6] = {L"C:\\Program Files\\SomeAppsSuiteFolder1", L"C:\\Program Files\\SomeAppsSuiteFolder2", L"C:\\Program Files (x86)\\SomeAppsSuiteFolder1", L"C:\\Program Files (x86)\\SomeAppsSuiteFolder2", L"C:\\SomeAppsSuiteFolder1", L"C:\\SomeAppsSuiteFolder2"};

Later on I use the array in for loop. The problem is, that for this line I get following errors:

error: too many initializers for 'wchar_t [6]'
error: initializer-string for array of chars is too long [-fpermissive]

What's more, in for loop I have if conditional like this one:

if(GetFileAttributesW(paths[i])!=INVALID_FILE_ATTRIBUTES) {...}

And, again, I get an error here:

error: invalid conversion from 'wchar_t' to 'LPCWSTR {aka const wchar_t*}' [-fpermissive]

Strange enough, similar code used to compile correctly some months ago... What's the problem?

burtek
  • 2,576
  • 7
  • 29
  • 37
  • 2
    A `wchar_t` is just a wide character, not a string. You're attempting to initialize an array of characters with (wide) string values. – Luchian Grigore Jul 03 '13 at 07:51

2 Answers2

7

You need

const wchar_t* paths[6] = ....
johnchen902
  • 9,531
  • 1
  • 27
  • 69
5

You need to use:

wchar_t *paths[6] = ...
        ^

A wchar_t is a single (wide) character, not a string of them.

So, if you want an array of wide strings, you should use the pointer variant.

The declarator wchar_t xyzzy[6] gives you six characters rather than six character arrays.

fatihk
  • 7,789
  • 1
  • 26
  • 48
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953