Pointer or sprintf - where's the problem?

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

Here's a code snippet.. b is a floating point variable

const char *c[16]={'xx'};
sprintf (*c, "Voltage %3.2f" , b);
LCD_Print(*c);

Puts's garbage on the LCD..
**BUT**

const char *c[16]={'xxxxxxx'};
sprintf (*c, "Voltage %3.2f" , b);
LCD_Print(*c);

works just fine.
It took a very long time to debug, but just initializing the *c with a longer value "fixes" the problem.
Any Ideas?
Just curious

Tom

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

Strictly speaking, both formulations are incorrect. The type of pointer table arrangement you've set up is to blame -- the memory map is almost certainly not what you are expecting:

First formulation
+-------------------------------------------+
|    c (table of 2 byte pointers)           |
|    +--------+                    +------+ |
|   0|pointer |------------------->|'xx\0'| |
|   1|NULL    |                    +------+ |
|   2|NULL    |                     3-byte  |
|   3|NULL    |                     buffer  |
|   4|NULL    |                             |
|   5|NULL    |                             |
|   6|NULL    |                             |
|   7|NULL    |                             |
|   8|NULL    |                             |
|   9|NULL    |                             |
|  10|NULL    |                             |
|  11|NULL    |                             |
|  12|NULL    |                             |
|  13|NULL    |                             |
|  14|NULL    |                             |
|  15|NULL    |                             |
|    +--------+                             |
+-------------------------------------------+

Second formulation
+------------------------------------------------+
|    c (table of 2 byte pointers)                |
|    +--------+                    +-----------+ |
|   0|pointer |------------------->|'xxxxxxx\0'| |
|   1|NULL    |                    +-----------+ |
|   2|NULL    |                     8-byte buffer|
|   3|NULL    |                                  |
|   4|NULL    |                                  |
|   5|NULL    |                                  |
|   6|NULL    |                                  |
|   7|NULL    |                                  |
|   8|NULL    |                                  |
|   9|NULL    |                                  |
|  10|NULL    |                                  |
|  11|NULL    |                                  |
|  12|NULL    |                                  |
|  13|NULL    |                                  |
|  14|NULL    |                                  |
|  15|NULL    |                                  |
|    +--------+                                  |
+------------------------------------------------+

This is correct:

char c[16];
sprintf(c, "Voltage %3.2f", b);
LCD_Print(c);
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Fair enough and point taken... I'll try but for curiosity, is this also valid (and if it is, it did not work which is why I went to the longer version)

*c[16]='x';
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

No, that formulation is not valid. In this case, you're trying to reserve a 16-entry table of pointers to chars, and initialize the first entry in that table with what effectively amounts to an integer, while leaving all the remaining 15 entries uninitialized and therefore pointing at "junk".

Initializing a pointer with an integer is a sure-fire way to generate a compile-time error.

And as I alluded to before, given the way in which you're using the array c, it's almost certain that you didn't really intend to declare it as an array of char-pointers, but rather as an array of chars.

Last Edited: Wed. Mar 28, 2007 - 07:03 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Ding! Ding!
Lightbulb goes off - clear as a bell now

Thanks lfmorrison.

Screech is on me ;)