Question

I have a string in C++ of type const char* passed as argument to strlen, but it returns void.

it goes like

strlen(astruct.string);

Thanks..

EDIT: Did some checking,

strlen("test");

still gives void.. why?

EDIT: Here's the image http://img14.imageshack.us/img14/1808/strlen.png

Sorry for being unclear previously. Formatting was not working quite well. Anyway, the confusion was solved by both Evan Teran and Vlad Romascanu. Also take a look at Brian R. Bondy's answer.

Thanks. Feel free to close.

Was it helpful?

Solution

Referring to your screen shot: your debugger is displaying <void> for strlen(...) when in fact it should display an error.

You cannot call methods and display their results in the debugger watch. The debugger will only display existing variables and data. It cannot invoke arbitrary methods on demand since the methods can alter the state of the program being debugged in ways that were not anticipated by either the author of the code nor by the debugger.

What you can do is, in your code, temporarily add:

size_t tmp_len = strlen(struc.string);

then compile, and add tmp_len to the watch.

OTHER TIPS

You are confused by the crappy debugger of visual studio 6.0. Don't rely on this. It likely couldn't get the return value due to inlining or something similar.

Print the value out to get the real value.

EDIT: Also, from your example, it seems that you may not be storing the result of strlen anyway. This also may be a reason why the debugger isn't seeing the return value. It's entirely possible that the compiler decided that it doesn't need to actually execute the strlen if you aren't using the value.

NOTE: at this point there is no real excuse for still using VC++ 6.0. It is an ancient compiler and IDE which is an embarrassingly poor c++ compiler. The newer versions of the visual c++ compiler are free (without the IDE), use them.

strlen is not of return type void, it's your debugger that is not giving the right message.

Why your debbuger is showing void?

The implementation of strlen that you are using is probably wrapped around a #define strlen someothername_strlen.

The debugger probably does not support #define properly or some other modifiers on the function.

You will have to do something like iLen = strlen("test") then check iLen in your watch.

Normally you can call functions in your watch. For example try to define the following function then call it in your watch:

int testFunc(char*)
{
  return 5;
}

You will probably get 5 in your watch as a result.

strlen returns an integer, so I assume you mean it returns "0".

Also, the way that you specify your data type, I can't quite tell if it's a const char * or const char **. If it's the latter, then you need to make sure you're dereferencing the ** to a single *.

My guess is that the string starts with a null byte, which is why it's returning 0.

In C++, functions always return a value of the type they are declared to return. Since the strlen function declaration looks something like this:

size_t strlen(const char *);

the only thing it can possibly return is a size_t. The compiler uses this information at compile time to determine how to handle the return value when you call the function. The point here is that if the strlen function is declared as above, it cannot decide to return void sometimes and a value of type size_t other times.

This is generally a characteristic of statically typed languages like C++. In dynamically typed languages (Perl, Python, Ruby, PHP, etc) a function can decide to return a value of any type each time it is called.

It can't return void. Void is the lack of a return value, so you can't, err, return it.

How do you check for void anyway? Void isn't a value. Please demonstrate how you are getting void. Is it compile time or run time?

If you do in fact have a system where strlen is declared with a void return type, run as fast as you can in the other direction.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top