'strlen' of a constant string


 
Thread Tools Search this Thread
Top Forums Programming 'strlen' of a constant string
# 1  
Old 08-13-2008
'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.
# 2  
Old 08-13-2008
What platform are you on and what header files are included?
# 3  
Old 08-13-2008
#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
# 4  
Old 08-13-2008
The code snippet you posted compiles and runs flawlessly on AIX. What happens if you remove the const qualifier...does it compile then.
# 5  
Old 08-13-2008
Taking the 'const' off gave the same results.
# 6  
Old 08-13-2008
FWIW - strlen returns a size_t, not an int.
# 7  
Old 08-13-2008
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')
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. Programming

Segment fault related to strlen.S

Hello, This function was copied into my code, which was compiled without error/warning, but when executed there is always Segmentation fault at the end after the output (which seems correct!): void get_hashes(unsigned int hash, unsigned char *in) { unsigned char *str = in; int pos =... (7 Replies)
Discussion started by: yifangt
7 Replies

2. Programming

String Constant C

I wonder string constant exists permanently or temporary. For example, printf("hello, world"); the function printf access to it is through a pointer. Does it mean storage is allocated for the string constant to exist permanently in memory? :confused: (4 Replies)
Discussion started by: kris26
4 Replies

3. Shell Programming and Scripting

How to solve awk: line 1: runaway string constant error?

Hi All ! I am just trying to print bash variable in awk statement as string here is my script n=1 for file in `ls *.tk |sort -t"-" -k2n,2`; do ak=`(awk 'FNR=='$n'{print $0}' res.dat)` awk '{print "'$ak'",$0}' OFS="\t" $file n=$((n+1)) unset ak doneI am getting following error awk:... (7 Replies)
Discussion started by: Akshay Hegde
7 Replies

4. Shell Programming and Scripting

Trouble appending string constant to variable

Hi. I define my variables as: month=jul DD=17 YEAR=2012 transmission_file_name_only=test_$month$DD$YEAR_partial.dat However when I run my script the variable 'transmission_file_name_only' resolves to: File "/downloads/test_jul17.dat" not found. How can I append this '_partial'... (3 Replies)
Discussion started by: buechler66
3 Replies

5. Programming

strlen for UTF-8

My OS (Debian) and gcc use the UTF-8 locale. This code says that the char size is 1 byte but the size of 'a' is really 4 bytes. int main(void) { setlocale(LC_ALL, "en_US.UTF-8"); printf("Char size: %i\nSize of char 'a': %i\nSize of Euro sign '€': %i\nLength of Euro sign: %i\n",... (8 Replies)
Discussion started by: cyler
8 Replies

6. Shell Programming and Scripting

choose random text between constant string.. using awk?

Hallo I have maybe a little bit advanced request.... I need to choose one random part betwen %.... so i have this.. % text1 text1 text1 text1 text1 text1 text1 text1 text1 % text2 text2 text2 text2 text2 % text3 text3 text3 tetx3 % this choose text between % awk ' /%/... (8 Replies)
Discussion started by: sandwich
8 Replies

7. Programming

Problems with Strlen

hello, i have a problem with strlen. I have written this: for(y=13,z=0; cInBuf!=' ';y++) { cBuf=cInBuf; z++; } len = strlen(cBuf); out=len/2; fprintf(outfile,"F%i",out); If strlen is e.g. 22, it write F22. I want to write F2F2. How can i do this?... (5 Replies)
Discussion started by: ACeD
5 Replies
Login or Register to Ask a Question