Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2230
  • Last Modified:

length of a char*

What's the easiest way to determine the length of a char*?
0
tblackwe
Asked:
tblackwe
1 Solution
 
MirkwoodCommented:
Depends on what you mean:
  sizeof(char *)
or
  char* p;
  strlen(p)
for the length of the string
0
 
viktornetCommented:
If you want to find out it manually you do this...

char *str;
int counter = 0;

while(*str++)counter++;

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...

-Viktor
--Ivanov
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now