Jump to content
  • Advertisement
Sign in to follow this  
3dmodelerguy

getting length of char*?

This topic is 3718 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

lets size i have char* test = "test character pointer"; Now this string is 22 characters long(maybe 23 with terminating character). Now would i be able to get the number through code?

Share this post


Link to post
Share on other sites
Advertisement
use the strlen(char*); or, assuming that the pointer is null terminated:

int size = 0;
for(size = 0; size < some max value; size ++)
{
if(test == (char)0)
break;
}

Share this post


Link to post
Share on other sites
You can also try this alternative if you're using C++:


char* test = "test character pointer";
std::string str(test);
std::cout << str.length(); // returns size of str

Share this post


Link to post
Share on other sites
Quote:
Original post by Great_White
You can also try this alternative if you're using C++:

*** Source Snippet Removed ***


Or you could just do:
std::string ("test character pointer").length ()

Share this post


Link to post
Share on other sites
Quote:
Original post by Key_46
use the strlen(char*); or, assuming that the pointer is null terminated:

int size = 0;
for(size = 0; size < some max value; size ++)
{
if(test == (char)0)
break;
}


dude, strlen requires a null terminated string,
and its the string that has to be null terminated, not the pointer.


and if you are working with char strings you should always null terminate them.




char* test = "test character pointer"; // const char strings will always be automatically null terminated
unsigned int size = strlen(test); // equals 22, null char not counted

include <string.h>, <windows.h> includes it in the background if you are working with windows.




Share this post


Link to post
Share on other sites
Quote:
Original post by Key_46
use the strlen(char*); or, assuming that the pointer is null terminated:

int size = 0;
for(size = 0; size < some max value; size ++)
{
if(test == (char)0)
break;
}

// alternatively
int size = 0;
while (*(str++))
size++;
return size;

Share this post


Link to post
Share on other sites
All proposed solutions work, but using strlen() is preferrable to everything else in the given example, because the string is a string literal.

Modern compilers will simply replace strlen() with an integer literal equal to the string's length, whereas the for() construct will calculate the length at runtime, as will std::string::length (which will additionally have to allocate memory, copy the string, and free memory).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!