int to char question

I would like to display some information on NKC display

this code does what you would think:

myGLCD.print("blue:", LEFT, 70,255,255,255);

I've googled to try and figure this question out but have not been successful.

I have several int variables that I need to display. This just displays garbage:

int chnlD = 1;
 char *c;
 c=(char *)chnlD;

Any suggestions would be great!
Thanks for checking in,
Loren

Use sprintf or itoa.

 char *c;
 c=(char *)chnlD;

c is a pointer. It doesn't point to anything. You can not put a value in the space it (doesn't) point to that way. You should not point to one type (int) with a pointer of another type (char), unless you really know what you are doing.

Thanks guys,

Here is what I've tried without any luck:

int red = 255;
sprintf(c, "%d", red);

Do I understand sprintf correctly?

Do I understand sprintf correctly?

Hard to say without seeing how you defined c.

sorry here it is:

int red = 255;
char *c;
sprintf(c, "%d", red);

Then, no, you are not using sprintf() correctly.

The way you have c defined, it is a pointer. What does it point to? Nothing. And, yet, you as sprintf() to write to the memory pointed to by c.

char c[4];
sprintf(c, "%d", red);

would work.

PaulS:
Then, no, you are not using sprintf() correctly.

The way you have c defined, it is a pointer. What does it point to? Nothing. And, yet, you as sprintf() to write to the memory pointed to by c.

char c[4];

sprintf(c, "%d", red);



would work.

It does work...but only partially

It does work...but only partially

That's...partially helpful

Sorry I was in the middle of four things.... for a moment it was only displaying a zero.

I had some other code botched. Now it is working as it should. Thanks for all of the help.