const int comment_begin_len = strlen(comment_begin);
const int comment_end_len = strlen(comment_end);
When I compile, I get the warnings:
emhttpc.c:64: warning: initializer element is not constant
emhttpc.c:64: warning: (near initialization for �comment_begin_len')
emhttpc.c:65: warning: initializer element is not constant
emhttpc.c:65: warning: (near initialization for �comment_end_len')
Taking the 'const' doesn't make any difference. I could use 'sizeof', but it gives one greater than the length (is it counting a null terminator?). I'm not sure why it says it's not constant as I'm doing a strlen on a pair of constant strings, so their length can't change.
emhttpc.c:65: warning: initializer element is not constant
emhttpc.c:65: warning: (near initialization for �comment_begin_len')
emhttpc.c:66: warning: initializer element is not constant
emhttpc.c:66: warning: (near initialization for �comment_end_len')
sizeof() is not the same as strlen(). sizeof() gives you the size of the array. Whereas strlen() counts the number of chars in the array until a '\0' is found. Your problem is totally related to the compiler (gcc in your case).
Those declarations are global right? Try declaring them global but without assigning the value of comment_begin_len and comment_end_len.
Do something like:
char comment_begin[] = "<!--";
char comment_end[] = "-->";
int comment_begin_len;
int comment_end_len;
...
int
main()
{
...
comment_begin_len = strlen (comment_begin);
comment_end_len = strlen(comment_end);
...
}
Yeah, that worked, I guess I should have done that earlier, but since those are constants, it just seems cleaner to assign them in the declaration line. I figured that, with strings, the null was being counted when using 'sizeof' since 'strlen' counts characters leading up to the null.
What about the runtime of strlen taken on constant strings instead of 'ordinary' strings? Shouldn't the compiler be able to tell the string's length during compilation and replace the strlen call with that value?
For example, if I write strlen("1234567890"), will the compiler replace it with (size_t)10, or will strlen be called every time that line is executed?