'strlen' of a constant string

In a declaration, I have:

const char comment_begin[] = "<!--";
const char comment_end[] = "-->";

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.

What platform are you on and what header files are included?

#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <resolv.h>
#include <sys/ioctl.h>
#include <sys/socket.h>

Ubuntu 8.04

The code snippet you posted compiles and runs flawlessly on AIX. What happens if you remove the const qualifier...does it compile then.

Taking the 'const' off gave the same results.

FWIW - strlen returns a size_t, not an int.

I have the constants now declared as:

const size_t comment_begin_len = strlen(comment_begin);
const size_t comment_end_len = strlen(comment_end);

and get the same result:

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')

And one more thing: why if you have a declaration like this:

char str[] = "teststring";

int str_l = sizeof(str);

will it set 'str_l' to 11, not 10? Is it counting the null byte?

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.

Anyway, I appreciate all of the input, gentlemen.

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?