C语言 在 C 中将一个数组的元素复制到另一个数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20371072/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 10:26:32  来源:igfitidea点击:

Copy elements of one array to another in C

carrayschar

提问by Rozen

So I am trying to use char array in C to copy from one array to another, each element at a time, however, I am stuck at this part. I did try to research but may be I'm a dumb or what not, I still could not get it right. Here is my code:

所以我试图在 C 中使用 char 数组从一个数组复制到另一个数组,一次每个元素,但是,我被困在这部分。我确实尝试过研究,但可能是我是个笨蛋还是什么不是,我仍然无法做对。这是我的代码:

Say I have two array

说我有两个数组

char arr1[1000][3]
char arr2[1000][3]

So wHat I mean is that [3] equal length of the string and each array has 1000 elements. So arr1[0] = "AB\0"

所以我的意思是 [3] 等长的字符串和每个数组有 1000 个元素。所以arr1[0] = "AB\0"

I can't, obviously, do arr1[i] = arr2[i];won't work of course, I did try strcpy but don't quite get it right. Please help me out a bit, thank you :)

我不能,显然,arr1[i] = arr2[i];当然不会工作,我确实尝试过 strcpy 但不太正确。请帮我一下,谢谢:)

Here a portion of it, the specific function I writing: Please just answer the one I ask, I can do this easily in Java but I really learning C so... An example would be nice :D

这是它的一部分,我编写的特定功能:请回答我问的问题,我可以用 Java 轻松完成此操作,但我真的在学习 C,所以......一个例子会很好:D

//build stacks                                                                                                                                                                                                     
card_pos_ptr add_card(card_pos_ptr head, char card_list[1000][3], int index){
  int i,k = 0;
  int start_index = index;
  card_pos_ptr current = head;
  for(i=0;i<13;i++){
    for(k=0;k<4;k++){
      if(current->card_stack == NULL){
        card_ptr node = (card_ptr)malloc(sizeof(card));
    strcpy(node->card_name,);
        node->up = false;
        node->down = NULL;
        start_index++;
      }else{
        card_ptr node = (card_ptr)malloc(sizeof(card));
        //node->card_name[0] = card_list[start_index];                                                                                                                                                             
        node->up = false;
        node->down = current->card_stack;
        current->card_stack = node;
        start_index++;
      }
    }
    current = current->next_pos;
  }

}

回答by Wald

Not sure what you are trying to do with this declaration but in frist place to declare array you need something like :

不确定你想用这个声明做什么,但在第一个地方声明数组你需要像这样的东西:

  char arr1[1000][3];
  char arr1[0][0] = 'A';
  char arr1[0][0] = 'B';
  ...

And in order to copy each element one at a time use a for loop:

为了一次复制一个元素,请使用 for 循环:

if ( something that determines X ? ) {
    for ( int i=0; i < 3 ; i++ ) {
       arr2[x][i] = arr1[x][i];
    }
}

回答by Cyborg-X1

I would do it like this:

我会这样做:

int i;
char arr1[1000][3];
char arr2[1000][3];

arr1[0][0]='v';

for(i=0;i<1000;++i)
{
    strncpy(arr2[i],arr1[i],3); //safe copy, copies max. 3 chars.
}

printf("%c\n",arr2[0][0]);

回答by Srikanth

Try

尝试

memcpy( &node->card_name[0] ,card_list[start_index])

memcpy( &node->card_name[0] ,card_list[start_index])