Find the size of a string pointed by a pointer

前端 未结 9 1061
一整个雨季
一整个雨季 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: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.

提交回复
热议问题