Go Down

Topic: Convert int to char and append to buffer (Read 1 time) previous topic - next topic

Riva

I have a char array that is partially filled with text and I want to convert an int to char and append to end of current text in the buffer. Is there a simple command to do this instead of converting int into another buffer and using strcat?

char buffer[100] = {"Something already there ="};

int number = 16;

//buffer=buffer+str(number)

// results in buffer containing "Something already there =16"

michael_x


Riva

#2
Jun 16, 2012, 04:05 pm Last Edit: Jun 16, 2012, 04:11 pm by Riva Reason: 1
Thanks michael_x,
I had almost got there with  itoa(x,chrBuffer[strlen(chrBuffer)],10); and wondering how to convert char to char* (I really don't get C programming with pointers).
Next question is how to append a space character?
Code: [Select]
   x=strlen(chrBuffer);
   chrBuffer[x] = " \0";

does not work.


majenko

Code: [Select]

    x=strlen(chrBuffer);
    chrBuffer[x] = ' ';
    chrBuffer[x+1] = '\0';

pekkaa

#4
Jun 16, 2012, 04:37 pm Last Edit: Jun 16, 2012, 04:41 pm by pekkaa Reason: 1
Code: [Select]
strcpy(buffer+strlen(buffer), " ");

Edit: fixed param order

pekkaa

Quote
I really don't get C programming with pointers.


I really think you should take the time and learn to understand the pointers. They are absolutely crucial in the C/C++ programming. And it's not that hard to learn really. 

michael_x

... the difference between char and char* is related to the difference between
'a' and "a" .
It's worth trying to understand that, too. 
Quote
And it's not that hard to learn really.
Yes :)

Riva

#7
Jun 16, 2012, 06:00 pm Last Edit: Jun 16, 2012, 06:05 pm by Riva Reason: 1
Quote
... the difference between char and char* is related to the difference between
'a' and "a" .

I understand the principal of pointers but it's the syntax that gets me.
I changed my code to use ' instead of " and it works but what is the special meaning of ' compared to " in C++
Also the arduino reference does not seem to mention little nuggets like itoa() and dtostrf(). Omissions like that steepens the learning curve.

majenko

' denotes a character.  " denotes a pointer to an array of characters.

' is equivalent to a char, " is equivalent to a char* or a char[].

Go Up