C 复制两个字符串数组

C 复制两个字符串数组,c,arrays,duplicate-data,C,Arrays,Duplicate Data,我有一个常量字符数组,我想更改(排序)里面的贵重物品。所以我需要创建第二个,它不是常量(我相信没有其他方法)。 该字段具有固定的列数(2),但没有固定的行数 所以首先我尝试了这个: count = 0; // count how many rows (i have 100% Null at the end) while(arrayConst[count][0]){ count ++; } char* arrayEditable [count][2]; // desl

我有一个常量字符数组,我想更改(排序)里面的贵重物品。所以我需要创建第二个,它不是常量(我相信没有其他方法)。 该字段具有固定的列数(2),但没有固定的行数

所以首先我尝试了这个:

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];
}
所以看起来我需要动态地分配它。 我试过这样的方法:

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];
}
我也相信,我把那个字段放错了,因为我不知道,那个字符串会有多长,这样它就可以溢出(但也许我弄错了)。
那么我应该如何复制该字段呢?我只需要不使用const字段来更改其中的值(sort)

您标记了这个C,并且使用了一个可变长度数组(VLA),这是一个C特性。代码的风格也是C,绝对不是C++。然而,编译器错误表明,您试图用C++编译器编译C代码,它是强>错误,,并且应该<强>立即停止这样做。<强> < /P> 用C编译器编译代码,错误就会消失


哦,当我们在做的时候:简单的不要。

啊,你是对的,btu这是给定的编译器,我必须使用这个。当我解决这个小问题时,它应该工作得很好;)@Luraye你不能在C++中使用VLAs。但是如果你必须使用C++,那么就写C++代码。使用<代码>矢量<代码> >代码>字符串,最多是代码>操作符new []/Cuff>等。不要乱搞VLAs和<代码> MalCube()/C>。如果他用C++编译器编译,他需要使用一个“代码> MalCube()/<代码>,我认为这个请求被夸大了。在C语言中,它不能被转换,因为它是多余的。在C++中,它不被强制使用,因为它根本不被使用。在C++中,字符串数组是“代码>向量”。代码>字符(*arrayEditable)[2]=(字符(*)[2])malloc(计数*2*sizeof(字符))不是你想在C++代码中看到的东西。那么,我们必须同意不同意。我只是发现,我可以直接改变那个const数组,所以我根本不需要去做。我以为你不能更改成本数组,但看起来你可以。。。。
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];
}
expected ‘const char *’ but argument is of type ‘char’|