Solved

length of a char*

Posted on 1999-01-22
2
2,207 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 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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
FMX TCameraComponent Problem 2 92
max float value 3 59
need help ...get stuck at middle of coding ...need to submit by Sunday C++ 1 44
C++ Code Issue 4 38
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

749 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