preprocessor length-of-string function

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

Is there a preprocessor function that returns the length of a string (constant)?

I have some code where I define a string using a macro:

#define	LCDrespondsOK	"LCD responds on I2C\r\nGood!"

and then I need to copy it to an I2C buffer in SRAM:

memcpy_P(&(i2cbuf[1]), PSTR(LCDrespondsOK), strlen(LCDrespondsOK));

I'm not sure how strlen works in this case (it compiles fine, but can't really find the sequence in the list file). I would instead prefer a preprocessor function for getting the length of a string. Is there such a function?

/Jakob Selbing

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

I think you're looking for the sizeof() operator. sizeof() is compile-time, though not
preprocessor-time. One thing to watch out for: strlen("string") is 6, but sizeof("string")
is 7, since it includes the trailing NUL byte.

Your strlen() call may be evaluated inline or at compile time, which could explain why
you can't find it.

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

Instead of memcpy_P, you could use strcpy_P. strcpy figures out the string length while performing the copy.

Don

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

mckenney wrote:
I think you're looking for the sizeof() operator. sizeof() is compile-time, though not
preprocessor-time. One thing to watch out for: strlen("string") is 6, but sizeof("string")
is 7, since it includes the trailing NUL byte.

Your strlen() call may be evaluated inline or at compile time, which could explain why
you can't find it.

OK, thanks. I did an empirical test, where I tried both using strlen and writing the string length directly (26). It occupies equal amount of flash, so I guess the strlen is compile-time...

Don - I think strcpy needs zero-termination, but that isn't what I have used here. Perhaps it would give more efficient code?

/Jakob Selbing

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

Quote:
I think strcpy needs zero-termination

Yes it does
Quote:
but that isn't what I have used here

Oh, yes You have. You don't need to state the terminating zero explicitly. If You write a literal string, eg. something like

"LCD responds on I2C\r\nGood!"

then the compiler will insert a zero termination. Thus codes such as

strlen( "LCD responds on I2C\r\nGood!")

is perfectly valid.

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

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

Yep, I didn't know that! Discovered it pretty soon though...

OK, here's another preprocessor question. I want to define a string, where one of the characters has the value of the length of a previously defined string.

#define	LCDrespondsOK	(LCD_FF "LCD responds on I2C" LCD_LF "Good!")
#define PressKeypad		(LCD_FF "Keypad data: 0x")
#define	PressKeypadFormat	("\xFE\x47" \sizeof(PressKeypad) "\x01")

The last row is what I am wondering about. It does not compile, which is what I expected. Can I make a decimal to string conversion within macros?

/Jakob Selbing

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

> Can I make a decimal to string conversion within macros?

Sorry, no, you can't.

Jörg Wunsch

Please don't send me PMs, use email if you want to approach me personally.