CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

sprintf not working

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
E_Blue



Joined: 13 Apr 2011
Posts: 417

View user's profile Send private message

sprintf not working
PostPosted: Wed Mar 08, 2017 9:24 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Mar 08, 2017 9:28 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Mar 08, 2017 10:22 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Mar 08, 2017 11:38 am     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Mar 09, 2017 6:35 am     Reply with quote

Thanks, clear as clean water. Smile
_________________
Electric Blue
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group