Find the size of a string pointed by a pointer

前端 未结 9 923
一整个雨季
一整个雨季 2020-12-08 20:39
#include 

int main ()
{
    char *ptr = \"stackoverflow\"

}

Is there any way to find the length of stackoverflow pointed by ptr, a

相关标签:
9条回答
  • 2020-12-08 20:48

    You can try using:

    char *ptr = "stackoverflow"
    size_t len = strlen(ptr);
    
    0 讨论(0)
  • 2020-12-08 20:55
    1. sizeof() returns the size required by the type. Since the type you pass to sizeof in this case is a pointer, it will return size of the pointer.

      If you need the size of the data pointed by a pointer you will have to remember it by storing it explicitly.

    2. sizeof() works at compile time. so, sizeof(ptr) will return 4 or 8 bytes typically. Instead use strlen.

    0 讨论(0)
  • 2020-12-08 20:56

    Even though this is a generic C question, it gets pretty high hits when looking this question up for C++. Not only was I in C/C++ territory, I also had to be mindful of Microsoft's Security Development Lifecycle (SDL) Banned Function Calls for a specific project which made strlen a no-go due to,

    For critical applications, such as those accepting anonymous Internet connections, strlen must also be replaced...

    Anyway, this answer is basically just a twist on the answers from the others but with approved Microsoft C++ alternative function calls and considerations for wide-character handling in respect to C99's updated limit of 65,535 bytes.

    #include <iostream>
    #include <Windows.h>
    int wmain()
    {
        //  1 byte per char, 65535 byte limit per C99 updated standard
        //  https://stackoverflow.com/a/5351964/3543437
        const size_t ASCII_ARRAY_SAFE_SIZE_LIMIT = 65535; 
    
        //  Theoretical UTF-8 upper byte limit of 6; can typically use 16383 for 4 bytes per char instead:
        //  https://stijndewitt.com/2014/08/09/max-bytes-in-a-utf-8-char/
        const size_t UNICODE_ARRAY_SAFE_SIZE_LIMIT = 10922;
    
        char ascii_array[] = "ACSCII stuff like ABCD1234.";
        wchar_t unicode_array[] = L"Unicode stuff like → ∞ ∑ Σὲ γνωρίζω τὴν ደሀ ᚦᚫᛏ.";
    
        char * ascii_array_ptr = &ascii_array[0];
        wchar_t * unicode_array_ptr = &unicode_array[0];
    
        std::cout << "The string length of the char array is: " << strnlen_s(ascii_array_ptr, ASCII_ARRAY_SAFE_SIZE_LIMIT) << std::endl;
        std::wcout << L"The string length of the wchar_t array is: " << wcsnlen_s(unicode_array_ptr, UNICODE_ARRAY_SAFE_SIZE_LIMIT) << std::endl;
    
        return 0;
    }
    

    Output:

    The string length of the char array is: 27
    The string length of the wchar_t array is: 47
    
    0 讨论(0)
  • 2020-12-08 21:01

    You are looking for the strlen() function.

    0 讨论(0)
  • 2020-12-08 21:02

    if ptr length is an argument of a function it's reasonable to use pointers as a strings. we can get string length by following code:

    char *ptr = "stackoverflow";
    length=strlen((const char *)ptr);
    

    And for more explanation, if string is an input string by user with variable length, we can use following code:

    unsigned char *ptr;
    ptr=(unsigned char *)calloc(50, sizeof(unsigned char));
    scanf("%s",ptr );
    length=strlen((const char *)ptr);
    
    0 讨论(0)
  • 2020-12-08 21:04

    The strlen() function provided by string.h gives you how many "real characters" the string pointed by the argument contains. However, this length does not include the terminating null character '\0'; you have to consider it if you need the length to allocate memory.

    That 4 bytes is the size of a pointer to char on your platform.

    0 讨论(0)
提交回复
热议问题