Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

length of a char*

Posted on 1999-01-22
2
Medium Priority
?
2,228 Views
Last Modified: 2008-02-26
What's the easiest way to determine the length of a char*?
0
Comment
Question by:tblackwe
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 13

Accepted Solution

by:
Mirkwood earned 100 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

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

610 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