SizeOf second awareness

xiaoxiao2021-03-06  14

Start my problem is

CHAR INTARRAY [] = "wo shi shui"; int LEN = SIZEOF INTARRAY; cout << len << Endl; output 12char * INTARRAY = "wo shi shui"; int LEN = SizeOf Intarray; cout << len << Endl; output 4

Why is an output 12, an output is 4. Because I reflected the length of the string by SizeOf and a string pointer, the case is not possible like the appearance above. If the length of the string can only return the number of bytes of the pointer.

Analysis of the reason: Individual thinks the pointer is a variable, which cannot be used to obtain a variable length, so this can only be obtained for the length of the variable type.

Here is the list of MSDNs

Sizeof: The sizeof operator yields the size of its operand with respect to the size of type char

When the sizeof operator is applied to an object of type char, it yields 1. When the sizeof operator is applied to an array, it yields the total number of bytes in that array, not the size of the pointer represented by the array identifier. TO OBTAIN THE SIZE OF THE POINTER REPRESENTED by The Array Identifier, Pass It As a parameter to a function That Uses Sizeof

#include

SIZE_T GETPTRSIZE (CHAR * PTR) {Return SizeOf (PTR);

Using namespace std; int main () {char szhello [] = "Hello, World!";

COUT << "The size of a char is:" << sizeof (char) << "/ nthe length of" << Szhello << "is:"

The size of the operand of the CHAR type can be obtained by the operation of the SIZEOF.

When SIZEO is used to operate the char object, it returns 1. When the SIZEOF is applied to an array that returns the number of bytes of the entire array, not the start of the array pointer (ie, the number of bytes of the pointer type, at the 32-bit operating system The byte tree of the pointer is 4), and the size of the pointer can be obtained as a parameter of the pointer.

转载请注明原文地址:https://www.9cbs.com/read-47967.html

New Post(0)