C语言sizeof陷阱
执行以下程序,查看输出:
#include <stdio.h>#define TOTAL_ELEMENTS (sizeof(array) / sizeof(array[0]))int array[] = {23,34,12,17,204,99,16};int main(){ int d; for(d=-1;d <= (TOTAL_ELEMENTS-2);d++) printf("%d\n",array[d+1]); return 0;}
MSDN上的解释为:
The sizeof keyword gives the amount of storage, in bytes, associated with a variable or a type (including aggregate types).
This keyword returns a value of type size_t.
其返回值类型为size_t,在头文件stddef.h中定义。这是一个依赖于编译系统的值,一般定义为
typedef unsigned int size_t;
sizeof( 2 );// 2的类型为int,所以等价于 sizeof( int );
sizeof( 2 + 3.14 ); // 3.14的类型为double,2也会被提升成double类型,所以等价于 sizeof( double );
C语言sizeof陷阱的更多相关文章
- C\C++ sizeof 陷阱&&总结
今天使用动态数组,本来想通过sizeof 获取动态数据,结果出现了错误. 先对自己做个测试,能做出下面这个题目,并做出合理解释,可以不用往下看了. ][]; cout<< cout< ...
- C语言-sizeof()与strlen()的区别【转】
先看看sizeof() 一.sizeof的概念 sizeof是C语言的一种单目操作符,如C语言的其他操作符++.--等.它并不是函数.sizeof操作符以字节形式给出了其操作数的存储大小.操作数可以是 ...
- c语言 sizeof理解
1.基本数据类型 char :1 short:2 int 4 long 4 long long :8 float:4 double :8字节. 2.数组:对应的基本数 ...
- C语言sizeof
一.关于sizeof 1.它是C的关键字.是一个运算符,不是函数: 2.一般用法为sizeof 变量或sizeof(数据类型):后边这种写法会让人误认为是函数,但这种写法是为了防止和C中类型修饰符(s ...
- c语言sizeof与strlen的区别
#include <stdio.h> #include <stdlib.h> #include <string.h> //strlen与sizeof的区别 //st ...
- C语言 - sizeof和strlen的区别
sizeof和strlen的区别: 1.sizeof操作符的结果类型是size_t,它在头文件中typedef为unsigned int类型. 该类型保证能容纳实现所建立的最大对象的字节大小. 2.s ...
- C 语言sizeof运算符
#include<stdio.h> int main() { ; ); ; int size3 = sizeof a; int size4 = sizeof(a); int size5 = ...
- C语言 sizeof()用法介绍
本文 转自https://www.cnblogs.com/huolong-blog/p/7587711.html 1. 定义 sizeof是一个操作符(operator). 其作用是返回 ...
- c语言sizeof用法(32位机)
随机推荐
- 用Bouncy Castle的C#版API产生公钥和私钥
开源API链接地址:The Legion of the Bouncy Castle Bouncy Castle,简称为BC,原本是java的一个开源JCE提供者,后来也提供了C#版本的API,我下载其 ...
- jsonp get 和 post
原文地址:http://blog.sina.com.cn/s/blog_4a7e719d0100zqzh.html jsonp获取服务器的数据,有两种一,跨域二,不跨域如果跨域js的写法有两种1,&l ...
- 【VNC】Ubuntu14.04LTS下安装VNC View
# apt-get install tightvncserver vnc4server gnome-panel gnome-settings-daemon metacity nautilus gnom ...
- Linux下编译UnixODBC
编译环境 操作系统: Red Hat Enterprise Linux Server release 5.4 64-bit 编译工具: gcc (GCC) 4.1.2 20080704 (Red Ha ...
- activiti自定义流程之Spring整合activiti-modeler5.16实例(一):环境搭建
项目中需要整合activiti-modeler自定义流程,找了很多资料后,终于成功的跳转到activiti-modeler流程设计界面,以下是记录: 一.整合基础:eclipse4.4.1.tomca ...
- Exception error message with incorrect line number
In Release mode the number in front of the exception is NOT the line of code. Instead it's an offset ...
- RAC_Oracle集群服务安装Grid Infrastructure(案例)
2015-01-24 Created By BaoXinjian Thanks and Regards
- BEvent_标准BusinessEvent用以监控供应商的修改(案例)
2014-06-01 Created By BaoXinjian
- CF109 C. Lucky Tree 并查集
Petya loves lucky numbers. We all know that lucky numbers are the positive integers whose decimal re ...
- python实现字体闪图
!/usr/bin/env python from future import print_function import os.path import sys from optparse impor ...