Thread: Copy int array into another int array

  1. #1
    Registered User
    Join Date
    Dec 2016
    Posts
    34

    Copy int array into another int array

    H,

    I need to Copy int array elements into another int array. Here is the function.

    Code:
    uint8_t SPI_Exchange8bitBuffer(uint8_t *dataIn, uint8_t bufLen, uint8_t *dataOut)
    {
        uint8_t bytesWritten = 0;
        extern volatile uint8_t buf[];
        if(dataOut != NULL)
                {
                    while(bytesWritten < bufLen )
                    {
                        dataOut[bytesWritten] = Send_data(dataIn);
    
    
                        bytesWritten++;
                    }
                    strncpy(buf, dataOut, bytesWritten);  //Is this correct?
                }
    }
    Will buf contain all received elements of dataOut? Is there any issue as buf is an int array and dataOut is a pointer to int array?

    --David

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Use memcpy, not strncpy. strncpy will copy at most the requested number of bytes but will stop early when it copies a zero byte. memcpy will always copy the requested number of bytes, even if they're all zeroes.

    In general, your code looks strange. What does Send_data do? If it only sends one byte then you're passing a pointer to the same byte every loop iteration. If it passes the whole dataIn array then it is doing it multiple times. And what is the return value?

  3. #3
    Registered User
    Join Date
    Dec 2016
    Posts
    34
    Hi Algorism...Thanks. I will use memcpy. I have not given full detail actually. uint8_t SPI_Exchange8bitBuffer will be called more than once from main function. Send_data does some operation on incoming dataIn every time and get dataOut. Incoming data dataIn will be like 0x11, 0x12, 0x13, 0x14. Same way dataOut will be like 0x66, 0x77, 0x88, 0x99. Now this dataOut I wanted to copy into separate uint8_t buf[] for further use. Let me know if it is fine.

  4. #4
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Quote Originally Posted by davidmeetsall View Post
    Let me know if it is fine.
    I can't follow that so I really don't know. It obviously depends on the details of the rest of your code.

    Does it compile?
    Does it run?
    Does it do what you expect?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 10-13-2013, 07:36 PM
  2. Replies: 1
    Last Post: 10-13-2013, 12:01 AM
  3. How to duplicating the Array & copy tokenPtr to an array
    By Watervase Chew in forum C Programming
    Replies: 21
    Last Post: 04-22-2013, 08:35 AM
  4. why can't a static array copy values from a dynamic array
    By c++noob145 in forum C++ Programming
    Replies: 2
    Last Post: 03-13-2013, 10:25 AM
  5. Copy multidimensional array to single array?
    By seepox in forum C Programming
    Replies: 9
    Last Post: 05-08-2006, 11:19 AM

Tags for this Thread