Pointer question - getting a char from a string array

Go To Last Post
4 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi Folks,

I'm trying to get a hold of the first char in a string array and write it to an LCD screen. I'm having an awful time and I'm pretty sure I bumbling up the pointers somewhere along the way. I know the correct string is stored in the array because I can print the whole string using a different function.

Prototype to write a char to the LCD:

void lcd_putc(char c);

Initialization of the variable:

char *deg_fraction_buffer[4];

Saving a string to the buffer (typical string would be "125"):

itoa((raw_temp%1000),(char *)deg_fraction_buffer,10);

I want to be able to display just the first char of this string. Here's how I'm trying to do it:

lcd_putc((char)*deg_fraction_buffer);

I keep getting this warning... the screen shows jibberish when I run this:

warning: cast from pointer to integer of different size

So I think I'm actually trying to display the address of the buffer rather than the value. But I'm stuck at this point because I thought I was appropriately dereferencing the pointer.

Can anyone shed light on this problem for me? Thanks!!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
char *deg_fraction_buffer[4]; 

should be

char deg_fraction_buffer[4]; 

Then the casts won't be needed. Realize that you have space for 3 chars and the zero byte. Anything over 999 will cause problems.

C: i = "told you so";

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

cpluscon wrote:

char *deg_fraction_buffer[4]; 

should be

char deg_fraction_buffer[4]; 


That's it, thank you! So, looking at this a bit I was initializing an array of pointers rather than an array of chars, is that correct?

Quote:
Then the casts won't be needed. Realize that you have space for 3 chars and the zero byte. Anything over 999 will cause problems.

I should have no problem with this limitation because I'm using modulus 1000 when setting that data. The result should never be greater than 999:

1546%1000 = 546
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
is that correct?

Correct.