简体   繁体   English

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

[英]Copy elements of one array to another in C

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. 因此,我尝试使用C语言中的char数组一次将一个数组中的每个元素复制到另一个数组中,但是我被困在这一部分。 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: 这是我的代码:

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. 所以我的意思是[3]字符串的长度相等,每个数组有1000个元素。 So arr1[0] = "AB\\0" 因此arr1[0] = "AB\\0"

I can't, obviously, do arr1[i] = arr2[i]; 我显然不能做arr1[i] = arr2[i]; won't work of course, I did try strcpy but don't quite get it right. 当然是行不通的,我确实尝试过strcpy,但是不太正确。 Please help me out a bit, thank you :) 请帮我一下,谢谢:)

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;
  }

}

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];
    }
}

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]);

Try 尝试

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试在C中使用“ strcpy”将char元素从一个数组复制到另一个数组时出现指针错误 - Pointer error when trying to use “strcpy” in C to copy char elements from one array to another 如何在C中将一个数组的数据复制到另一个数组? - How to copy data of one array to another in C? 如何在 C 编程中将一个数组复制到另一个数组 - How to copy one array to another in C programming ansi-c,将数组元素更好地复制到另一个数组? - ansi-c, better copy array elements to another array? 使用C语言将数组中的元素范围复制到另一个数组中 - copy range of elements from an array to another array in C language 编写一个 C 程序将 char 数组元素从一个数组复制到另一个数组,而不必担心空字符 - Make a C program to copy char array elements from one array to another and don´t have to worry about null character C - 使用指针将一个char数组的内容复制到另一个char数组 - C - Copy contents of one char array to another using pointers 如何使用指针算法在C中将值从一个数组复制到另一个数组 - How to copy values from one array to another in C with pointer arithmetic 如何在数组中查找一个值并将其复制到 C 语言中的另一个值? - How to find a value in array and copy it to another one in C language? 如何使用C中的指针将一个数组的内容复制到另一个数组? - How to copy contents of one array to another using pointers in C?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM