Thomas Fjellstrom <[EMAIL PROTECTED]> writes: >> > Change sizeof to strlen here (and in the corresponding statement below); >> > sqlite3_bind_text wants the length of the string, not the size of the >> > pointer to it. >> >> sizeof(SEX)-1 is fine. > > I hope you mean strlen(SEX)-1 > > sizeof is a compile time macro like operator, it returns the size of the > thing you pass, be it a normal variable or a pointer, and since literal > strings evaluate to a pointer, you'll get sizeof(char *), or 4 on x86.
The "thing you pass" in this case is actually an array of characters, not a pointer. The array of characters contains the letters in the string, plus the null terminator. x.c: ------------------------------------------------------------------------------ #include <string.h> #define TESTSTR "hello world" int main(int argc, char * argv[]) { printf("sizeof(%s) = %d\n", TESTSTR, sizeof(TESTSTR)); printf("strlen(%s) = %d\n", TESTSTR, strlen(TESTSTR)); return 0; } ------------------------------------------------------------------------------ % cc -o x x.c % x sizeof(hello world) = 12 strlen(hello world) = 11 % So either pass sizeof(TESTSTR)-1 which is the "fast" way since the calculation of the length is determined at compile time; or pass strlen(TESTSTR) which calculates the length of the string at run time. sizeof requires subtracting one to account for the null terminator byte which sizeof includes but isn't wanted in sqlite3_bind_text().