length of a char*

What's the easiest way to determine the length of a char*?
Who is Participating?
MirkwoodConnect With a Mentor Commented:
Depends on what you mean:
  sizeof(char *)
  char* p;
for the length of the string
If you want to find out it manually you do this...

char *str;
int counter = 0;


counter contains the lenght of str, but without the NULL char at the end..

Note: This should be in a function, but you should create a different pointer that will keep the location of the original place of the pointer (the beginning of the string) since str would point to the end of the string when everything is done.... Here is how it should look..

int str_len(char *str)
  int count = 0;
  while (*str++) count++;
  return count;

Hope this helps...

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.