View previous topic :: View next topic |
Author |
Message |
E_Blue
Joined: 13 Apr 2011 Posts: 417
|
sprintf not working |
Posted: Wed Mar 08, 2017 9:24 am |
|
|
I'm trying to add text to a string buffer
Code: | sprintf(USB_TxBuffer[offsetX],";ID=%s;#%s;*",ID,LastSentence); |
ID is declared as char[5]
LastSentence as char[5]
USB_TxBuffer as char[128]
ID contains a string of four char and ID[4]=0, also LastSentence
USB_TxBuffer is ended with a 0x0A,0x0D,0x00 and offsetX is pointing correctly to the last added character of USB_TxBuffer.
So, What I'm doing wrong? _________________ Electric Blue |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19513
|
|
Posted: Wed Mar 08, 2017 9:28 am |
|
|
sprintf, expects to receive a _pointer_ to where the data is meant to go. So (for instance) USB_TxBuffer.
USB_TxBuffer[offsetX] is not a pointer. It's the value contained 'at' OffsetX inside the array USB_TxBuffer.
If you want to print 'to' the location 'offsetX' in the buffer you need:
&USB_TxBuffer[offsetX]
or
USB_Txbuffer+offsetX |
|
|
E_Blue
Joined: 13 Apr 2011 Posts: 417
|
|
Posted: Wed Mar 08, 2017 10:22 am |
|
|
Thanks for your fast and clear answer.
I understand what you just explain but Why if I just write USB_TxBuffer it works ok and write starting at the very first byte of USB_TxBuffer?
Should I use &USB_TxBuffer instead when I want to write the string starting at offset zero? _________________ Electric Blue |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19513
|
|
Posted: Wed Mar 08, 2017 11:38 am |
|
|
No.
In 'C', the name of an array (with no brackets or index), is the pointer to it. It's a standard feature of the language. So USB_TxBuffer, is equivalent to &USB_TxBuffer[0]. |
|
|
E_Blue
Joined: 13 Apr 2011 Posts: 417
|
|
Posted: Thu Mar 09, 2017 6:35 am |
|
|
Thanks, clear as clean water. _________________ Electric Blue |
|
|
|