invalid initializer

1
2
3
4
5
6
7
8
9
void WriteRegistry(char MyPath[MAX_PATH])
{
    // Write into registry
    HKEY hKey;
    RegCreateKey(HKEY_LOCAL_MACHINE,"SOFTWARE\\Test",&hKey);      
    const unsigned char szRegPath[MAX_PATH] = MyPath;
    RegSetValueEx(hKey,"test",0,REG_SZ,szRegPath,sizeof(szRegPath));
    RegCloseKey(hKey);
}


void main(argv[]) passes the argv[0] but the bold line gives "invalid initializer" error! what is wrong? please help!
Last edited on
You cannot copy a string like that. Either use lstrcpy() or just cast MyPath to the correct thing:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void WriteRegistry(TCHAR MyPath[MAX_PATH])
{
    // Write into registry
    HKEY hKey;
    if (RegCreateKey(HKEY_LOCAL_MACHINE,TEXT("SOFTWARE\\Test"),&hKey) != ERROR_SUCCESS) return;
    RegSetValueEx(
        hKey,
        TEXT("test"),
        0,
        REG_SZ,
        (CONST BYTE*)MyPath,
        lstrlen( MyPath )
        );
    RegCloseKey(hKey);
}

Hope this helps.
thanks, it worked flawlessly!
can you please exlpain what i did wrong?
Essentially this:
1
2
3
4
5
6
int main()
  {
  int one[ 6 ] = { 1, 2, 3, 4, 5, 6 };
  int two[ 6 ];
  two = one;
  }

That just won't work. You can't copy an entire array with an assignment statement.

You can copy the address of an array with an assignment, since an address is a pointer:
1
2
3
4
5
6
7
8
int main()
  {
  int one[ 6 ] = { 1, 2, 3, 4, 5, 6 };
  int *two;
  two = one;
  // which is the same as
  two = &(one[ 0 ]);
  }

Since two is now a pointer (not an array) you can make it point to anything, including the first element in an array.

Or, you can copy the array:
1
2
3
4
5
6
int main()
  {
  int one[ 6 ] = { 1, 2, 3, 4, 5, 6 };
  int two[ 6 ];
  memcpy( two, one, 6 * sizeof( int ) );
  }


Hope this helps.


Oh, I forgot to mention that all that TEXT() and TCHAR stuff is <windows.h> boilerplate to make things work if you ever compile for Unicode. You are just using ASCII or ISO-8859-1 so it shouldn't make a difference as long as you don't turn the Unicode compile flag on.

:-)
Last edited on
Topic archived. No new replies allowed.