C / C ++如何复制没有嵌套循环的多维字符数组? [英] C / C++ How to copy a multidimensional char array without nested loops?

查看:160
本文介绍了C / C ++如何复制没有嵌套循环的多维字符数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个聪明的方式来复制一个多维字符数组到一个新的目的地。我想复制char数组,因为我想编辑内容而不改变源数组。



我可以构建嵌套循环来手工复制每个字符,但我希望有是更好的方法。



更新:



尺寸2.水平尺寸。只有长度(行)。



代码如下所示:

  char ** tmp; 
char ** realDest;

int length = someFunctionThatFillsTmp(& tmp);

//现在我想将tmp复制到realDest

找到一个方法,将tmp的所有内存复制到可用内存并指向realDest。



更新2: b
$ b

someFunctionThatFillsTmp()是来自Redis C lib的函数credis_lrange() credis.c



在lib tmp中创建:

  rhnd-> reply.multibulk.bulks = malloc(sizeof(char *)* CR_MULTIBULK_SIZE)

更新3:



我试图在这行中使用memcpy:

  int cb = sizeof(char)* size * 8; // string inside 2. level has 8 chars 
memcpy(realDest,tmp,cb);
cout<< realDest [0]<< endl;

print:mystring

但我得到了:程序接收到的信号:EXC_BAD_ACCESS

解决方案

您可以使用 memcpy



如果在编译时给出多维数组大小,即 mytype myarray [1] [2] ,则只需要一个memcpy调用

  memcpy(dest,src,sizeof(mytype)* rows * coloumns); 

如果像你指出的那样动态分配数组,你需要知道两者的大小尺寸作为动态分配时,数组中使用的内存不会在连续的位置,这意味着memcpy将不得不使用多次。



给定一个2d数组,复制它的方法如下:

  char ** src; 
char ** dest;

int length = someFunctionThatFillsTmp(src);
dest = malloc(length * sizeof(char *));

for(int i = 0; i // width必须已知(见下文)
dest [i] = malloc宽度);

memcpy(dest [i],src [i],width);
}

从你的问题看来,你正在处理一个字符串数组,您可以使用 strlen 查找字符串的长度(It必须为null终止)。



在这种情况下,循环将变为

  for(int i = 0; i  int width = strlen(src [i])+ 
dest [i] = malloc(width);
memcpy(dest [i],src [i],width);
}


I'm looking for a smart way to copy a multidimensional char array to a new destination. I want to duplicate the char array because I want to edit the content without changing the source array.

I could build nested loops to copy every char by hand but I hope there is a better way.

Update:

I don't have the size of the 2. level dimension. Given is only the length (rows).

The code looks like this:

char **tmp;
char **realDest;

int length = someFunctionThatFillsTmp(&tmp);

//now I want to copy tmp to realDest

I'm looking for a method that copies all the memory of tmp into free memory and point realDest to it.

Update 2:

someFunctionThatFillsTmp() is the function credis_lrange() from the Redis C lib credis.c.

Inside the lib tmp is created with:

rhnd->reply.multibulk.bulks = malloc(sizeof(char *)*CR_MULTIBULK_SIZE)

Update 3:

I've tried to use memcpy with this lines:

int cb = sizeof(char) * size * 8; //string inside 2. level has 8 chars
memcpy(realDest,tmp,cb);
cout << realDest[0] << endl;

prints: mystring

But I'm getting a: Program received signal: EXC_BAD_ACCESS

解决方案

You could use memcpy.

If the multidimensional array size is given at compile time, i.e mytype myarray[1][2], then only a single memcpy call is needed

memcpy(dest, src, sizeof (mytype) * rows * coloumns);

If, like you indicated the array is dynamically allocated, you will need to know the size of both of the dimensions as when dynamically allocated, the memory used in the array won't be in a contiguous location, which means that memcpy will have to be used multiple times.

Given a 2d array, the method to copy it would be as follows:

char** src;
char** dest;

int length = someFunctionThatFillsTmp(src);
dest = malloc(length*sizeof(char*));

for ( int i = 0; i < length; ++i ){
    //width must be known (see below)
    dest[i] = malloc(width);

    memcpy(dest[i], src[i], width);
}

Given that from your question it looks like you are dealing with an array of strings, you could use strlen to find the length of the string (It must be null terminated).

In which case the loop would become

for ( int i = 0; i < length; ++i ){
    int width = strlen(src[i]) + 1;
    dest[i] = malloc(width);    
    memcpy(dest[i], src[i], width);
}

这篇关于C / C ++如何复制没有嵌套循环的多维字符数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆