2

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

}
14
  • 1
    memcpy ? Commented Dec 4, 2013 at 9:09
  • 1
    how u do that? Like memcpy(arr1[i], arr2[i], 3);??? Commented Dec 4, 2013 at 9:16
  • Yeah, but it would be better to copy the entire array with a single call to memcpy, unless there's something preventing you from doing that. Commented Dec 4, 2013 at 9:18
  • Yes, I don't simply copy one array to another. What I am doing is copy content of each element in the big array and pass it in the small array in the struct. Commented Dec 4, 2013 at 9:20
  • 1
    The 1000x3 is genuine 2D, not to be confused with the pointer-array idiom most people call 2D. this one is actual 2D. The ones in the cards are 1D (obviously). Anyway, assuming each (both the 1000-card shoe and the nodes you're creating) are indeed made of 3-char arrays, there is no reason you can't memcpy() or strcpy() the data. Since you know they're all 2-char strings with a terminator, strcpy() is somewhat pointless. I'd memcpy() it over if I was told to do it this way. Commented Dec 4, 2013 at 9:37

3 Answers 3

4

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];
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

No, each element in the char array contains more than 1 char, so it will be AB, not just A or B. Plus I tried assigning like ur example, the compiler say incompatible type assignment.
1 dimensional is different from 2 dimensional array
Like I said i just wasn't sure what was he doing i answered before he edited his question. Does this help you ?
This one would work fine too I guess, Beside the one answers under my question :) Thank you!
1

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

1 Comment

appreciate your answer!
0

Try

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

1 Comment

need 3 arguments for memcpy.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.