Reputation: 139
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;
}
}
Upvotes: 2
Views: 27661
Reputation: 1091
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:
if ( something that determines X ? ) {
for ( int i=0; i < 3 ; i++ ) {
arr2[x][i] = arr1[x][i];
}
}
Upvotes: 4
Reputation: 373
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]);
Upvotes: 1