Advertisement

finding the length of a char*

Started by November 25, 2002 02:27 AM
6 comments, last by billybob 21 years, 11 months ago
i''m totally stumped by this, how do you find the length of the string at the end of a pointer? i have char * s, and i can''t seem to find the length of it. i need it for TextOut(), and so far i''ve only made it work manually, as in counting the characters. how would i do this?
Asuming your string is null terminated you can use the strlen() function.
Advertisement
how do i make it null terminated?
Make sure the string ends with a \0 that will insert a null charecter, of course if you hard code the string like this..

char* text = "a sentence"

then it will be null terminated by the compiler
is it not already?
quote: Original post by billybob
how do i make it null terminated?

Please tell me you''re kidding. You are kidding, right?
Advertisement
Wow, a question I know how to answer. In C, something like "blah" represents the data ''b'', ''l'', ''a'', ''h'', 0. The 0 was there to show that the string had ended.

len = 0;
while(str[len] != 0)
len++;

After the loop is complete, len will have your length. The strlen function is also a quick way to computer the length of this type of string. I don''t think strlen() was included in ANSI C (though I could be wrong), but it is availabe for you in the newer C++ compilers.
Or what? You'll send the dogs? Or the bees? Or the dogs with bees and when they bark they shoot bees at you?
heh, i''m totally self taught, i went from djgpp and allegro strait to directx and windows programming, so i''ve been stumbling about the sdk docs and msdn for the whole time i''ve been programming. i skipped all the basics, so i don''t know a lot of the simple stuff i should know

strlen in string.h worked

This topic is closed to new replies.

Advertisement