memcpy - part of array to specific position in different array

This is a discussion on memcpy - part of array to specific position in different array within the C Programming forums, part of the General Programming Boards category; Hello, I am trying to do a simple program like this. There is an input, which is a wide-char message ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    9

    memcpy - part of array to specific position in different array

    Hello,

    I am trying to do a simple program like this. There is an input, which is a wide-char message and i should copy only some substrings of this message to the output. In the example below, the output should be input[4]+input[3..5]. So lets say for wcMessageIn='ABCDEFG' I should get wcMessageOut='DCDE'.

    Code:
    #define MSG_LENGTH 4096
    
    static wchar_t *wcMessageIn;    
    static wchar_t *wcMessageOut; 
    
    int main(int argc, char *argv[])                 
    {
      wcMessageIn = (wchar_t *)malloc(MSG_LENGTH * sizeof(wchar_t));
      wcMessageOut = (wchar_t *)malloc(MSG_LENGTH * sizeof(wchar_t));
    
      /* receive message and convert it to wcMessageIn using mbstowcs */
      /* lMsgWCharLen = mbstowcs(wcMessageIn, cMessage, MSG_LENGTH);  */
    
      /* copy 4th character on input to 1st character on output - THIS WORKS*/
      wcMessageOut[0] = wcMessageIn[3];
    	
      /* append 3th to 5th character on input to output - THIS DOES NOT WORK */
      memcpy(wcMessageOut + 1*sizeof(wchar_t), wcMessageIn + 2*sizeof(wchar_t), 3*sizeof(wchar_t));
    	
      /* do some cleanup here */
    }
    Can you please help me with the memcpy? In the real world, the messages will be long and I would be copying substrings of hundreds or thousands characters, so I would like to avoid doing it in a loop, char by char.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    There's no need to multiply your address offsets by sizeof(wchar_t) - pointer arithmetic will do it for you.

    Or you could do
    Code:
    memcpy( &wcMessageOut[1], &wcMessageIn[2], 3*sizeof(wchar_t));
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Something like...
    Code:
    memcpy(out,in + start_offset, total_len - end -start_offset);

  4. #4
    Registered User
    Join Date
    Feb 2011
    Posts
    9
    Salem, thank you very much. That helped a lot!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Duplicating specific binary data in array
    By chris.lloyd in forum C Programming
    Replies: 1
    Last Post: 10-21-2006, 09:54 PM
  2. comapring specific locations in an array
    By mbooka in forum C Programming
    Replies: 6
    Last Post: 02-26-2006, 04:01 PM
  3. Array Program
    By emmx in forum C Programming
    Replies: 3
    Last Post: 08-31-2003, 12:44 AM
  4. Creating 2D arrays on heap
    By sundeeptuteja in forum C++ Programming
    Replies: 6
    Last Post: 08-16-2002, 11:44 AM
  5. mode of an array
    By Need Help in forum C Programming
    Replies: 15
    Last Post: 09-17-2001, 08:03 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21