How to set quantity of elements of the char array of a variable

How to set quantity of elements of the char array of a variable?

This is my code:

long int len = strlen(qq);
//char buff[10];
//sprintf(buff, "%d", len);

char key[len] = "12345678901234567890123456789..";//error
char crypt[len];//error

for (int i = 0; i < len; i++) {
     crypt[i] = text[i] ^ key[i];


In C++ an array can only be staticly sized using a constant variable that is known at compile time, which your len is not. Note that some compiler extensions DO allow this, as it's allowed in C. But for pure C++ you instead can use dynamic memory allocation (this allocates on the heap instead of the stack):

char* key = new char[len];
char* crypt = new char[len];

Note that a char* can be used the same as a char[] (you can still use array indexing, the rest of your code remains unchanged).

Because the array is now allocated on the heap, to avoid memory leaks you must manually free the memory when you no longer need it using delete e.g. at the end of the function after you loop:

delete[] key;
delete[] crypt;

Since you specified the winapi tag, it may be that the CString class would be a viable solution.

For your key though, you don't even need to allocate an array dynamically. You could use the modulo operator:

static const char key[] = "1234567890";
const size_t keyLength = strlen(key);

CString crypt(0, len);

for (int i = 0; i < len; i++) {
  crypt[i] = text[i] ^ key[i & keyLength];

Need Your Help

How to develop UI which will look native to Android using jQuery-mobile?

android jquery-mobile cordova themeroller

I am trying to build an Android application using Phonegap and jQuery Mobile. To customize UI, I am using ThemeRoller. While using ThemeRoller, I noticed that by default controls have look and feel...

Is it possible to have a QMaemo5ListPickSelector which displays images as items?

c++ qt

I wanted to know if its possible to have a QMaemo5ListPickSelector but one which displays rectangular images as list items instead of text ? Is this possible ? If not, is there any other list-like ...