所以我试图在C中使用char数组从一个数组复制到另一个数组,每次一个元素,但是,我被困在这个部分。 我确实尝试过研究,但可能是我愚蠢或不是,我仍然无法做到正确。 这是我的代码:
说我有两个阵列
char arr1[1000][3] char arr2[1000][3]所以我的意思是[3]字符串的长度相等,每个数组有1000个元素。 所以arr1[0] = "AB\0"
我显然不能做arr1[i] = arr2[i]; 当然不会工作,我确实尝试过strcpy但是做得不对。 请帮我一点,谢谢:)
这里是它的一部分,我写的具体功能:请回答我问的那个,我可以用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; } }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:
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"
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 :)
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
//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; } }最满意答案
不确定你要对这个声明做什么,但是在第一个声明数组的地方你需要这样的东西:
char arr1[1000][3]; char arr1[0][0] = 'A'; char arr1[0][0] = 'B'; ...并且为了一次复制一个元素,请使用for ...
if ( something that determines X ? ) { for ( int i=0; i < 3 ; i++ ) { arr2[x][i] = arr1[x][i]; }}
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...
if ( something that determines X ? ) { for ( int i=0; i < 3 ; i++ ) { arr2[x][i] = arr1[x][i]; }}
更多推荐
发布评论