简体   繁体   中英

Confusion about returning string length

I rewrote the strlen() function from the <string.h> header.

Calling the function using the code below produces a ridiculously high number eg 6356735

#include <stdio.h>
#include <string.h>

size_t my_strlen(const char *s);

int main(void) {
    const char str[] = "abcd";

    printf("length of abcd: %d\n", my_strlen(str));
}

size_t my_strlen(const char *s) {
    const char *p = s;
    while (*s)
        s++;
    return s;
}

Changing the return statement of my_strlen to return s - p; gives the correct string length.

If I'm not mistaken, p points to the beginning of the string, so subtracting p from s should be the same as s ? How does this work in particular?

Here, you're returning the string (a pointer to char ) instead of the length of the string:

size_t my_strlen(const char *s)
{
    const char *p = s;
    while(*s)
        s++;
    return s; // here
}

Instead, do this (subtract the end of the string from the beginning):

size_t my_strlen(const char *s)
{
    const char *p = s;
    while(*s)
        s++;
    return s-p; // here
}

The strings aren't stored at zero, they're stored in a location in memory (somewhere that's not zero, and depends on the environment).

For example, take this: char *p1 = "helloworld"; char *p2 = "goodbye"; p1 = p2 + 5; char *p1 = "helloworld"; char *p2 = "goodbye"; p1 = p2 + 5; . p1 does not point to world at the end of this, it points to ye .

p points to the beginning of the string but it's a pointer not an index. It will never be zero since that is what is used when malloc() fails to allocate memory.

Pointers are confusing indeed.

The example you provided actually shows the value of the address of memory to which a pointer s points. The value is 'random'.

The operator ++ (both pre and post increment) of a char pointer shifts a pointer sizeof(char) bytes to the right (ie shifts the pointer to the next char).

As an example, let char* p point to some memory address 123. If we incremented it (ie p++ ), it would point to the memory address 123 + sizeof(char) (summing to 124).

p points to the beginning of the string

Yes. It points to an address of memory where a char which is the beginning of the string resides.

subtracting p from s should be the same as s

Usually no. It is only true if the size of string is zero (assuming that a pointer is not NULL). In the code you provided, pointer s gets incremented n times. An increment is adding sizeof(char) to a char pointer. For that reason, s becomes p + n * sizeof(char) . p + n * sizeof(char) is equal to p only if n is zero.

How does this work in particular?

We've discovered that s is p + n * sizeof(char) . The operation s - p may be expanded as ((p + n * sizeof(char)) - p)/sizeof(char) , which is n - the size of string.

Note: c-style strings are \\0 terminated. \\0 is interpreted as false , and it ends the while loop at the end of a string.

You may play a bit with http://pythontutor.com/c.html#mode=edit so as to understand pointers better.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM