Solved

length of a char*

Posted on 1999-01-22
2
2,195 Views
Last Modified: 2008-02-26
What's the easiest way to determine the length of a char*?
0
Comment
Question by:tblackwe
2 Comments
 
LVL 13

Accepted Solution

by:
Mirkwood earned 50 total points
ID: 1184864
Depends on what you mean:
  sizeof(char *)
or
  char* p;
  strlen(p)
for the length of the string
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1184865
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now