0

i have const char array and i want to change (sort) valuables inside it. SO i need to create a second one which will not be const (i believe there is no other way). The field has fixed number of columns (2), but not number of rows.

So first i tried this:

count = 0;           // count how many rows (i have 100% Null at the end)
while(arrayConst[count][0]){
    count ++;
}

char* arrayEditable [count][2]; // deslare new field

for(i = 0; i < count; i++){    // and copy everithing to new one
    arrayEditable[i][0] = (char*)arrayConst[i][0];
    arrayEditable[i][1] = (char*)arrayConst[i][1];
}

This works pretty well (program is running) except i have this message from compilator:

ISO C++ forbids variable length array ‘arrayEditable’

So it looks like i need to allocate that dynamicly. I tried something like this:

count = 0;
while(arrayConst[count][0]){
    count ++;
}

char (*arrayEditable)[2];
arrayEditable = (char (*)[2])malloc(count * 2 * sizeof(char));

for(i = 0; i < count; i++){
    arrayEditable[i][0] = arrayConst[i][0];
    arrayEditable[i][1] = arrayConst[i][1];
}

but it still doesnt work - now im getting some weird info:

expected ‘const char *’ but argument is of type ‘char’|

and i also believe, that i alocated that field wrong, because i dont know, how long will that string be so it can overflow (but maybe i get it wrong). So how should i duplicate that field? I just need not const field to change values in it (sort);

Kulikjak
  • 95
  • 1
  • 9
  • Ou i just find out, that i can change that const array straight away so i dont need to dupe it at all. I thought that you cannot change cost array, but it looks like you can.... – Kulikjak Dec 01 '13 at 12:07

1 Answers1

1

You tagged this C and you are using a variable-length array (VLA) which is a C feature. The style of the code is also C, and definitely not C++. Yet your compiler error suggests that you are trying to compile C code with a C++ compiler, which is wrong, and you should immediately cease doing so.

Compile your code with a C compiler and the error will be gone.

Oh, and while we're at it: do not ever cast the return value of malloc()! Simply don't.

Community
  • 1
  • 1
  • Ah you are right btu this is given compiler and i have to use this one. It should work pretty good, when i will solve this little problem ;) – Kulikjak Nov 30 '13 at 18:55
  • @ßluray You can't use VLAs in C++. But if you are obliged to use a C++, then write C++ code. Use `vector`, `string`, **at most** `operator new[]`, etc. Don't mess around with VLAs and `malloc()`. –  Nov 30 '13 at 19:02
  • If he's compiling with a C++ compiler, he'll need to use a cast on `malloc()`, and I think the proscription is overblown. – Jonathan Leffler Nov 30 '13 at 19:19
  • @JonathanLeffler No. `malloc()` is never to be cast. In C it shan't be cast because it's superfluous. In C++, it shan't be cast, because it is not to be used at all. An array of strings is `vector` in C++. `char (*arrayEditable)[2] = (char (*)[2])malloc(count * 2 * sizeof(char));` is not something you want to see in C++ code. –  Nov 30 '13 at 19:33
  • We're going to have to agree to disagree, then. – Jonathan Leffler Nov 30 '13 at 21:05