关于大小的字符数组与字符指针
Character Arrays versus Character Pointers regarding size
引用我读过的一篇文章:
*...考虑以下两个变量:
char s6[ ] = "hello", *s7 = "hello";
s6
恰好为 6 个字节分配 space; s7
为 10(通常)分配 space - 6 个字符加上另外 4 个指针变量。*
想知道谁拥有 s6
(数组中第一个字符)的地址? s6
'save' 指针的 4 个字节如何?
s6 allocates space for exactly 6 bytes; s7 allocates space for 10 ( typically ) - 6 for the characters plus another 4 for the pointer variable.
不,那是不正确的。
s6
正好有 6 个字节的 space,s7
有指针的 space(大小)(通常为 4 或 8 个字节,具体取决于您的体系结构),它指向用于初始化它的字符串。
换句话说,
s6
的大小是 sizeof ("hello")
。
-
s7
的大小是 sizeof (s7)
,即 sizeof (char *)
您可以执行以下程序来检查尺寸:
#include <stdio.h>
int main(void)
{
char s6[ ] = "hello", *s7 = "hello";
printf("s6 = %zu\n", sizeof (s6));
printf("s7 = %zu\n", sizeof (s7));
return 0;
}
在我的系统上,它们产生:
s6 = 6 // sizeof ("hello"), or , sizeof (char [6])
s7 = 8 // sizeof (char *)
简而言之,它不会存储在您的程序中的任何位置。只有编译器会跟踪它。
幕后花絮:
s6
表示"address XXXXXXXX: a block of six bytes, holding the values 'H', 'e', 'l', 'l', 'o', 0
"
s7
表示 "address YYYYYYYY: a block of four bytes, holding the values ZZ, ZZ, ZZ, ZZ
"
*s7
表示 "address ZZZZZZZZ: a block of one byte, holding the value 'H'
"
程序实际上不必在任何地方存储值 XXXXXXXX; 编译器 只是在您使用 s6
.
的任何地方插入值 XXXXXXXX
同样,程序不必在任何地方存储 YYYYYYYY,但它 确实 存储 ZZZZZZZZ,因为你特意这么说了(你说要将值 ZZZZZZZZ 赋给变量s7
).
不过,如果您想将 XXXXXXXX 存储在某处,您可以轻松地这样做:
char my_pointer* = &s6;
现在 my_pointer
表示 "address WWWWWWWW: a block of four bytes, holding the values XX, XX, XX, XX
"。
P.S。这是假设你在一个有 four-byte 指针的系统上;现在,指针更可能是八个字节或 64 位。
Curious about to who holds the address of s6 (of the first char in the
array)? And how does s6 'save' the 4 bytes for the pointer?
编译器/链接器持有这个地址。它被视为常量,因为它不能在 运行 时间被修改。希望对您有所帮助。
引用我读过的一篇文章:
*...考虑以下两个变量:
char s6[ ] = "hello", *s7 = "hello";
s6
恰好为 6 个字节分配 space; s7
为 10(通常)分配 space - 6 个字符加上另外 4 个指针变量。*
想知道谁拥有 s6
(数组中第一个字符)的地址? s6
'save' 指针的 4 个字节如何?
s6 allocates space for exactly 6 bytes; s7 allocates space for 10 ( typically ) - 6 for the characters plus another 4 for the pointer variable.
不,那是不正确的。
s6
正好有 6 个字节的 space,s7
有指针的 space(大小)(通常为 4 或 8 个字节,具体取决于您的体系结构),它指向用于初始化它的字符串。
换句话说,
s6
的大小是sizeof ("hello")
。-
s7
的大小是sizeof (s7)
,即sizeof (char *)
您可以执行以下程序来检查尺寸:
#include <stdio.h>
int main(void)
{
char s6[ ] = "hello", *s7 = "hello";
printf("s6 = %zu\n", sizeof (s6));
printf("s7 = %zu\n", sizeof (s7));
return 0;
}
在我的系统上,它们产生:
s6 = 6 // sizeof ("hello"), or , sizeof (char [6])
s7 = 8 // sizeof (char *)
简而言之,它不会存储在您的程序中的任何位置。只有编译器会跟踪它。
幕后花絮:
s6
表示"address XXXXXXXX: a block of six bytes, holding the values'H', 'e', 'l', 'l', 'o', 0
"s7
表示 "address YYYYYYYY: a block of four bytes, holding the valuesZZ, ZZ, ZZ, ZZ
"*s7
表示 "address ZZZZZZZZ: a block of one byte, holding the value'H'
"
程序实际上不必在任何地方存储值 XXXXXXXX; 编译器 只是在您使用 s6
.
同样,程序不必在任何地方存储 YYYYYYYY,但它 确实 存储 ZZZZZZZZ,因为你特意这么说了(你说要将值 ZZZZZZZZ 赋给变量s7
).
不过,如果您想将 XXXXXXXX 存储在某处,您可以轻松地这样做:
char my_pointer* = &s6;
现在 my_pointer
表示 "address WWWWWWWW: a block of four bytes, holding the values XX, XX, XX, XX
"。
P.S。这是假设你在一个有 four-byte 指针的系统上;现在,指针更可能是八个字节或 64 位。
Curious about to who holds the address of s6 (of the first char in the array)? And how does s6 'save' the 4 bytes for the pointer?
编译器/链接器持有这个地址。它被视为常量,因为它不能在 运行 时间被修改。希望对您有所帮助。