Go Down

Topic: int to char question (Read 778 times) previous topic - next topic

Loren

I would like to display some information on NKC display

this code does what you would think:

Code: [Select]
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:
Code: [Select]
int chnlD = 1;
char *c;
c=(char *)chnlD;


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

mkwired


PaulS

Code: [Select]
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.

Loren

Thanks guys,

Here is what I've tried without any luck:

Code: [Select]
int red = 255;
sprintf(c, "%d", red);

Do I understand sprintf correctly?

PaulS

Quote
Do I understand sprintf correctly?

Hard to say without seeing how you defined c.

Loren

sorry here it is:

Code: [Select]
int red = 255;
char *c;
sprintf(c, "%d", red);

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.
Code: [Select]

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

would work.

Loren


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.
Code: [Select]

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

would work.


It does work...but only partially

AWOL

Quote
It does work...but only partially

That's...partially helpful
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Loren

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.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy