C language > Beginner questions
lstrlen
(1/1)
Grincheux:
Why when a string is defined as a constant the c compiler calls the lstrlen function? It knows its length at compile time. I don't understand.
Example :
--- Code: ---static void Main_OnPaint(HWND hwnd)
{
PAINTSTRUCT ps;
RECT rc;
int _i = lstrlen("Hello, Windows!") ;
BeginPaint(hwnd, &ps);
GetClientRect(hwnd, &rc);
DrawText(ps.hdc, _T("Hello, Windows!"), -1, &rc, DT_SINGLELINE|DT_CENTER|DT_VCENTER);
EndPaint(hwnd, &ps);
}
--- End code ---
The asm code is :
--- Quote ---lea rcx,[StringAdr]
call qword ptr [__imp_lstrlenA]
--- End quote ---
Is there an other way to compute a string length in that case.
I have strings as the following:
--- Code: ---char str1[] = "String" ;
char str2[] = "\n"
"a file to \"generate\""
"%s\n" ;
--- End code ---
From Pelle's help file :
--- Quote ---6. Adjacent string literal tokens are concatenated into one string literal token ("abc" "def" becomes "abcdef").
--- End quote ---
An other question. In the previous code I have noticed that if a string exists more than once, it is created only once. Please confirm.
Grincheux:
The only way to compute string length when the string is known at compile time is :
int _i = sizeof(szString) / sizeof(char) ;
TimoVJL:
you presumably mean:
int _i = sizeof(szString) / sizeof(TCHAR) ;
Grincheux:
Yes >:(
Pelle:
--- Quote from: Grincheux on January 02, 2020, 05:54:24 PM ---Why when a string is defined as a constant the c compiler calls the lstrlen function?
--- End quote ---
Because all Windows functions are just that: functions. Black boxes. Unknown what the function does.
Use one of the standard C functions, that the compiler have any reason to care about, like strlen() or wcslen().
--- Quote from: Grincheux on January 02, 2020, 05:54:24 PM ---It knows its length at compile time.
--- End quote ---
No it doesn't. See above.
Navigation
[0] Message Index
Go to full version