1, C语言 sizeof用法以及需要特别注意的地方;
2, sizeof 与 strlen()的区别;
要想弄明白这两点其实很简单,先来看下 sizeof 的定义
The C Programming Language 写道
C provides a compile-time unary operator called sizeof that can be used to compute the size of any object. The expressions
sizeof object
and sizeof(type name)
generates an integer to the size of the specified object or type in in bytes.
.
MSDN 写道
The sizeof operator gives the amount of storage,in bytes, required to store an object of the type of the operand.
.
C语言 基本数据类型(primary data type)有int,double,char,long,可以看下面的代码来知道这些基本类型的size
void test_size(void)
{
printf("sizeof(int):%d\n",sizeof(int));
printf("sizeof(double):%d\n",sizeof(double));
printf("sizeof(char):%d\n",sizeof(char));
printf("sizeof(long):%d\n",sizeof(long));
}
sizeof(int): 4
sizeof(double): 8
sizeof(char) : 1
sizeof(long) : 4
对于pointers(指针),可以看下面代码
void pointers_size(void)
{
char *p = "Chuan Su";
int *p1;
int a = 9;
p1 = &a;
double *p2;
double b = 9.00;
p2 = &b;
char **p3;
p3 = &p;
printf("sizeof(p):%d\n",sizeof(p));
printf("sizeof(p1):%d\n",sizeof(p1));
printf("sizeof(p2):%d\n",sizeof(p2));
printf("sizeof(p3):%d\n",sizeof(p3));
}
sizeof(p): 4
sizeof(p1): 4
sizeof(p2): 4
sizeof(p3): 4
由上面的代码可以看出,指针变量所占的内存与指针所指的对象没有任何关系,均为 4 bytes。
对于 sizeof "array" 数组的size,先看下面代码
void array_size(void)
{
char string[] = "Chuan Su";
int a[5];
printf("sizeof(string):%d\n",sizeof(string));
printf("lengthof(string):%d\n",strlen(string));
printf("sizeof(a):%d\n",sizeof(a));
}
sizeof(string): 9
lengthof(string): 8
sizeof(a): 20
首先来看array所占的内存,再来看sizeof与strlen的区别
The C Programming Language 写道
When a string constant like "hello\n" appears in a C program, it is stored as an array of characters containing characters of the string and terminated with a '\0' to mark the end.
所以char string[] = "Chuan Su"的所占的内存,即size为
(8+1)*1 = 9;
而 int a[5]所占的内存,即size为
5*4 = 20 // ( 5*sizeof(int)= 20 bytes );
如果想要得到 int a[5]的长度,可以这样 sizeof(a)/sizeof(int)
说到这里,我想请大家注意的一点是,先看下面的代码
void array_size(char string[8])
{
printf("sizeof(string):%d\n",sizeof(string));
printf("lengthof(string):%d\n",strlen(string));
}
sizeof(string): 4
lengthof(string): 8
这里对形参char string[8] 所得到的size是 4 而非上面所说的 9,在" C专家编程 " 一书中在分析指针和数组的时候作了很好的解释
C专家编程 写道
C语言允许程序员把形参声明为数组(程序员打算传递给函数的东西)或者指针(函数实际所接收到的东西)。编译器知道何时形参是作为数组声明的,但事实上在函数内部,编译器始终把它当作一个指向数组第一个元素(元素长度未知)的指针。
因此很有意思,没有办法把数组本身传递给一个函数,因为它总是被转换为指向数组的指针。当然,在函数内部使用指针,所能进行的对数组的操作几乎跟传递原原本本的数组没有差别。只不过,如果想用 sizeof(实参)来获取数组的长度,所得结果不正确而已。
所以此处所求为 指针的size而非数组的size
下面来看下sizeof和strlen()的区别
二者有本质上的区别 从上面的定义可以知道sizeof只是一个operator,而strlen()则是定义一个定义在<string.h>中的函数;所以sizeof(string)是在计算string所占用的内存,包含了'\0'结尾符,strlen(string)则是用来计算字符串的长度,省略了'\0'
接下来,看下SIZE OF STRUCTURES。 在 "Programming in ANSI C" 一书对此有如下解释
Programming in ANSI C 写道
The expression sizeof(struct x)
will evaluate the number of bytes required to hold all the members of the struct x. If y is a simple structure variable of struct x,then the expression
sizeof(y) would give the same answer.However,if y is an array of variable of type struct x,then the sizeof(y)
would give the total number of bytes the array y requires.The expression
sizeof (y) / sizeof (x) or sizeof (y) / sizeof y[0] would give the number of elements in the array y
讲了这么多,总之,对sizeof用法的理解,还是回归定义,仔细理解下文章开头的两处定义的引用,以后是不会用错的。
刚开始学C语言,先写到这里,以后有新的发现再补上。
分享到:
相关推荐
C语言sizeof用法[借鉴].pdf
析Sizeof的用途
详细解析C语言中的sizeof详细解析
sizeof是C语言的一种单目操作符,如C语言的其他操作符++、–等,sizeof操作符以字节形式给出了其操作数的存储大小。操作数可以是一个表达式或括在括号内的类型名。这个操作数不好理解对吧?后面慢慢看就明白了。...
主要介绍了sizeof的概念以及相关用法,我辛辛苦苦找的,希望对大家有所帮助
4.3 C语言sizeof运算符 23 4.4 C语言逻辑运算符实例讲解 23 4.5 C语言?:条件运算符实例讲解 24 4.6 C语言关系运算符实例讲解 25 4.7 C语言表达式和语句 26 第5章 C语句和程序流 27 5.1 C语言表达式和语句 27 5.2 ...
sizeof是C语言的一种单目操作符,如C语言的其他操作符++、--等。它并不是函数。sizeof操作符以字节形式给出了其操作数的存储大小。操作数可以是一个表达式或括在括号内的类型名。操作数的存储大小由操作数的类型决定...
c语言中sizeof的用法.pdf
解析C语言中的sizeof.docx
程序通过简洁的代码展示了如何使用sizeof来查找各种变量类型的大小,这是C语言编程中重要的一环,非常适合初学者进行学习。 程序的主要目标是确定各种类型的变量在内存中的大小。 程序的开头,我们包含了stdio.h...
C语言里sizeof的用法
主要介绍了C语言中sizeof()与strlen()函数的使用入门及对比,同时二者在C++中的使用情况也基本上同理,是需要的朋友可以参考下
一、sizeof的概念 ;二、sizeof的使用方法 ;三、sizeof的结果 ;四、sizeof与其他操作符的关系 ;五、sizeof的主要用途 ;六、建议 。
不用C中关键字sizeof 计算int型的字节数 要求编写sizeof函数
以下是对C语言中sizeof的细节进行了详细的分析介绍,需要的朋友可以参考下
该文件详细介绍了用sizeof求取struct结构体内存大小的各种情况
sizeof()和strlen()经常会被初学者混淆,但其中有有很大区别: sizeof() 1. sizeof()【操作数所占空间的字节数大小】是一种c中的基本运算符。 可以以类型、指针、数组和函数等作为参数。 头文件类型为unsigned int。...
对c语言中sizeof进行总结!!sizeof的概念 sizeof的使用方法 、sizeof的结果 sizeof与其他操作符的关系
主要介绍了C语言中的sizeof操作符用法及和strlen的区别,本文讲解了sizeof的定义、sizeof的语法以及使用实例,最后给出sizeof与strlen的区别,需要的朋友可以参考下