Home > Blockchain >  Calculate length of string using pointers with (non-char)
Calculate length of string using pointers with (non-char)

Time:03-28

I'm working on an exercise to calculate the length of a string using pointers. Here's the code I've written below:

int main() {

    std::string text = "Hello World";
    std::string *string_ptr = &text; 
    int size = 0;

   //Error below: ISO C   forbids comparison between pointer and integer [-fpermissive]
    while (string_ptr != '\0') {
        size  ;
        string_ptr  ;
    }
    std::cout << size;
}

In a lot of examples that I've seen, the string is often a char array which I also understand is a string. However, I want to try calculate it as a string object but I'm getting the error below.

Is it possible to calculate it where the string is an object, or does it need to be a char array?

CodePudding user response:

If you just want the size of the string, well, use std::string::size():

auto size = text.size();

Alternatively, you can use length(), which does the same thing.

But I'm guessing you're trying to reimplement strlen for learning purposes. In that case, there are three problems with your code.

First, you're trying to count the number of characters in the string, and that means you need a pointer to char, not a pointer to std::string. That pointer should also point to constant characters, because you're not trying to modify those characters.

Second, to get a pointer to the string's characters, use its method c_str(). Getting the address of the string just gets you a pointer to the string itself, not its contents. Most importantly, the characters pointed to by c_str() are null terminated, so it is safe to use for your purposes here. Alternatively, use data(), which has been behaving identically to c_str() since C 11.

Finally, counting those characters involves checking if the value pointed to by the pointer is '\0', so you'll need to dereference it in your loop.

Putting all of this together:

const char* string_ptr = text.c_str(); // get the characters
int size = 0;

while (*string_ptr != '\0') { // make sure you dereference the pointer
    size  ;
    string_ptr  ;
}

CodePudding user response:

You are totally confused here.

“text” is a std::string, that is an object with a size() method retuning the length of the string.

“string_ptr” is a pointer to a std::string, that is a pointer to an object. Since it is a pointer to an object, you don’t use text.size() to get the length, but string_ptr->size().

So first, no, you can’t compare a pointer with an integer constant, only with NULL or another pointer.

The first time you increase string_ptr it points to the memory after the variable text. At that point using *string_ptr for anything will crash.

Remember: std::string is an object.

CodePudding user response:

First things first, the problem is irrelevant. std::string::size() is a O(1) (constant time) operation, as std::string's typically store their size. Even if you need to know the length of a C-style string (aka char*), you can use strlen. (I get that this is an exercise, but I still wanted to warn you.)

Anyway, here you go:

size_t cstrSize(const char* cstr)
{
    size_t size(0);
    while (*cstr != '\0')
    {
          size;
          cstr;
    }
    return size;
}

You can get the underlying C-style string (which is a pointer to the first character) of a std::string by calling std::string::c_str(). What you did was getting a pointer to the std::string object itself, and dereferencing it would just give you that object back. And yes, you need to dereference it (using the * unary operator). That is why you got an error (which was on the (string_ptr != '\0') btw).

  • Related