简体   繁体   English

为什么sizeof运算符给出不同的输出

[英]Why does the sizeof operator give different outputs

I am learning the basics of C for a course I am taking soon, and right now I am learning about pointers and arrays. 我正在为即将上的一门课程学习C的基础知识,现在我正在学习有关指针和数组的知识。 I have been using a number of references to learn it but in this "Head First into C" book I have, there is the following: 我一直在使用许多参考资料来学习它,但是在我拥有的“ Head First into C”这本书中,包含以下内容:

char s[] = "How big is it?"
char *t = s;
printf("%i\n", sizeof(s));  /* returns 15 */
printf("%i\n", sizeof(t));  /* returns 4 or 8 */

Thats all fine, but if i change the last line to: 没关系,但是如果我将最后一行更改为:

printf("%i\n", sizeof(*s));  /* returns 1 (on my compy anyway) */

So my question is why is this different? 所以我的问题是为什么这有区别? Apologizes if it's a really basic thing, but I have yet to wrap my brain around the concept of pointers. 抱歉,如果这确实是最基本的事情,但是我还没有为指针的概念而绞尽脑汁。

One is the size of an array and the other is the size of a pointer. 一个是数组的大小,另一个是指针的大小。

Arrays and pointer are different types in C although there are implicit conversion rules between arrays and pointers.. 数组和指针在C中是不同的类型,尽管数组和指针之间存在隐式转换规则。

The size of an array initialized by a character string literal is the number of characters of the string + 1 for the null terminator. 由字符串文字初始化的数组的大小是空终止符的字符串的字符数+1。

The size of a pointer to char (ie, char * ) is an implementation defined size of an object large enough to hold the pointer value. 指向char的指针(即char * )的大小是对象定义的实现大小,该对象的大小足以容纳指针值。

This statement 这个说法

printf("%i\n", sizeof(s));  /* returns 15 */

outputs the number of bytes that that array s occupies. 输出该数组s占用的字节数。

This statement 这个说法

printf("%i\n", sizeof(t));  /* returns 4 or 8 */

displays the number of byte occupied by pointer t. 显示指针t占用的字节数。 Usually pointers have size either 4 or 8 bytes depending of using system. 通常,指针的大小取决于使用的系统,为4或8个字节。

This statement 这个说法

printf("%i\n", sizeof(*s));  /* returns 1 (on my compy anyway) */

displays sizeof char because expression *s has type char. 显示sizeof char因为表达式*s类型为char。 In all system size of an object of type char is equal to 1. 在所有系统中,char类型的对象的大小等于1。

sizeof(s) returns the size of the char array, s , which is 14 char s long, plus one for the null terminator; sizeof(s)返回char数组s的大小,该数组的长度为14 char s,再加上一个表示空终止符; ergo 15 bytes. ergo 15个字节。

sizeof(t) returns the size of the char pointer t . sizeof(t)返回char指针t的大小。 t isn't aware of s 's length, because when you assign its value from s , s decays from an array type to a pointer type, losing the length information in the process. t并不知道s的长度,因为当您从s分配s值时, s会从数组类型衰减为指针类型,从而丢失了长度信息。 As you have discovered, pointers can vary in size depending on the target platform. 如您所见,指针的大小可能会因目标平台而异。

sizeof(*s) and sizeof(*t) both dereference the address of the first char in the array, yielding a single char . sizeof(*s)sizeof(*t)都取消引用数组中第一个char的地址,从而产生单个char sizeof(char) == 1, because a char is per definitionem a single byte. sizeof(char) == 1,因为按定义, char是单个字节。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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