Problem with pointer copy in C

Posted by Stefano Salati on Stack Overflow See other posts from Stack Overflow or by Stefano Salati
Published on 2011-01-09T16:58:16Z Indexed on 2011/01/09 17:53 UTC
Read the original article Hit count: 163

Filed under:
|
|

I radically re-edited the question to explain better my application, as the xample I made up wasn't correct in many ways as you pointed out:

I have one pointer to char and I want to copy it to another pointer and then add a NULL character at the end (in my real application, the first string is a const, so I cannot jsut modify it, that's why I need to copy it).

I have this function, "MLSLSerialWriteBurst" which I have to fill with some code adapt to my microcontroller.

tMLError MLSLSerialWriteBurst( unsigned char slaveAddr, 
                           unsigned char registerAddr, 
                           unsigned short length, 
                           const unsigned char *data )
{
unsigned char *tmp_data;

  tmp_data = data;
  *(tmp_data+length) = NULL;

  // this function takes a tmp_data which is a char* terminated with a NULL character ('\0')
  if(EEPageWrite2(slaveAddr,registerAddr,tmp_data)==0)
    return ML_SUCCESS;
  else 
    return ML_ERROR;

}

I see there's a problem here: tha fact that I do not initialize tmp_data, but I cannot know it's length.

© Stack Overflow or respective owner

Related posts about c

    Related posts about string