Thread: Cant figure it out.

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    110

    Cant figure it out.

    Is there a fast way to set 2 unsigned char arrays equal to eachother?

    Right now im just running a for loop which sets x[i] = y[i] but is there a way to do


    unsigned char Xarray[4];
    unsigned char Yarray[4];

    Xarray = Yarray;

    ?

  2. #2
    Registered User
    Join Date
    May 2005
    Posts
    28
    Quote Originally Posted by Coder87C
    Is there a fast way to set 2 unsigned char arrays equal to eachother?

    Right now im just running a for loop which sets x[i] = y[i] but is there a way to do


    unsigned char Xarray[4];
    unsigned char Yarray[4];

    Xarray = Yarray;

    ?
    It is more C'ish but you can use memcpy or memset.


    Mezzano

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    memcpy(Xarray,YArray,sizeof(YArray));
    beware that if Yarray is a pointer and not an array you'll need so specify the size of the contained buffer on memcpy last paramater

  4. #4
    Registered User
    Join Date
    Jul 2003
    Posts
    110
    Works like a charmed ANgel

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If you're looking for all-out speed, then unroll the loop (some compilers will do this for you)

    Code:
    Xarray[0] = Yarray[0];
    Xarray[1] = Yarray[1];
    Xarray[2] = Yarray[2];
    Xarray[3] = Yarray[3];
    The problem with say memcpy() is that it is likely to be a function call, which can get expensive in comparison if you're only copying small blocks of data.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Something like this can be amusing.
    Code:
       struct block
       {
          unsigned char array[4];
       };
       *(struct block *)Xarray = *(struct block *)Yarray;
    [edit]Hmm. For a Win32 console app using BC55, it makes pretty tiny assembly code; copying all 4 characters at once via a 4-byte register:
    Code:
    	mov       eax,dword ptr [_Yarray]
    	mov       dword ptr [_Xarray],eax
    Last edited by Dave_Sinkula; 05-20-2005 at 01:58 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    It might seem ridiculous for a large array, but your loop idea is fine. The CPU's little brain can only handle one variable (one element of the array) at a time. ...No matter what technique you use, the machine code has to step-thru the array.

    EDIT-
    Oh, with character arrays (null-terminated C-style strings) you can use the strcpy() or strncpy() functions in the <cstring> header to copy one character array into another. These functions do the looping for you.
    Last edited by DougDbug; 05-20-2005 at 01:58 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Figure out how method was called?
    By jcafaro10 in forum C++ Programming
    Replies: 2
    Last Post: 02-07-2009, 10:43 AM
  2. 3 dimensional figure volume
    By thekautz in forum C++ Programming
    Replies: 2
    Last Post: 01-20-2009, 05:22 PM
  3. trying to figure out someone's code for a plugin
    By paulpars in forum C++ Programming
    Replies: 4
    Last Post: 07-20-2006, 10:57 AM
  4. newb to C, cant figure something out.
    By Phate4219 in forum C Programming
    Replies: 16
    Last Post: 03-06-2006, 01:47 AM
  5. ahh i can't figure out this loop
    By blindleaf in forum C Programming
    Replies: 1
    Last Post: 03-18-2003, 09:42 AM