惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

The Hacker News
The Hacker News
GbyAI
GbyAI
雷峰网
雷峰网
罗磊的独立博客
WordPress大学
WordPress大学
博客园_首页
Hugging Face - Blog
Hugging Face - Blog
The Cloudflare Blog
云风的 BLOG
云风的 BLOG
F
Full Disclosure
Google DeepMind News
Google DeepMind News
C
Cyber Attacks, Cyber Crime and Cyber Security
NISL@THU
NISL@THU
S
Schneier on Security
T
Tor Project blog
C
Cybersecurity and Infrastructure Security Agency CISA
Recent Announcements
Recent Announcements
酷 壳 – CoolShell
酷 壳 – CoolShell
C
Check Point Blog
P
Palo Alto Networks Blog
C
CERT Recently Published Vulnerability Notes
S
Secure Thoughts
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Last Week in AI
Last Week in AI
T
Threatpost
I
Intezer
Y
Y Combinator Blog
G
GRAHAM CLULEY
MyScale Blog
MyScale Blog
阮一峰的网络日志
阮一峰的网络日志
T
The Exploit Database - CXSecurity.com
Scott Helme
Scott Helme
A
Arctic Wolf
Martin Fowler
Martin Fowler
Hacker News: Ask HN
Hacker News: Ask HN
V
V2EX
B
Blog RSS Feed
The Last Watchdog
The Last Watchdog
博客园 - 司徒正美
Simon Willison's Weblog
Simon Willison's Weblog
V
Vulnerabilities – Threatpost
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
N
News and Events Feed by Topic
www.infosecurity-magazine.com
www.infosecurity-magazine.com
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
P
Proofpoint News Feed
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
AI
AI
C
Cisco Blogs
T
The Blog of Author Tim Ferriss

博客园 - BearOcean

LOG.ZS.0001.基于Freetype的游戏字体渲染优化思路 const 和指针 C++ 下啥时候用struct, 啥时候用class 解决站点关键数据,状态数据,无须持久化数据的一些思路 BS程序代码与安全与基本攻击/防御模式 Struts 实现的I18N Ant 阅读笔记 进度,效率,与个人事务管理 Personal Task 1.0 MySql与Java的时间类型 数据挖掘概述 解决Thread 的关闭问题和参数传递时想到的办法. Command 模式 .Net标准控件与自定义控件(2) ToolTipButton 内网聊天工具FreeChat 2.0 FreeChat 2.0 ...大改 模型和架构 局域网聊天工具FreeChat 1.0 开发日志 内网聊天工具FreeChat Beta 为Socket写的附加方法 .Net 事件
C++ 和 Java 中的变参
BearOcean · 2009-03-09 · via 博客园 - BearOcean

● 测试代码:

MSDN 上的范例代码,说实话写得很烂。并没有说明变参实现中,几个重要宏的特性。

Code


运行结果:
 15.

● 说明:

C 语言的可变参数实际上是通过
   va_list
   va_arg
   va_end 宏实现的。

  其中 va_list 宏可以取得变参中的第一个参数。 (需要指明变参前一个参数)
       va_arg 取得变参中的一个参数, 并向下移动指针到下一个参数位置。

    由于C 的实现方式, 所以变参函数在实现的时候,都无法确定va_arg 的取值范围。
    所以都需要在对变参进行解析之前,明确变参的实际数量。

    例如:

    printf(char* format, ...);
    看起来似乎并没有传入变参的数量, 但实际上 format 中包含的格式字符串已经说明了后面变参的数量,和类型。
    如 "%d,%s,%s" 指明一共3个参数, 第一个是整数, 后面两个是字符串。

  在Java 中, 变参方法似乎要方便一些。

  如: print(String ... args)
  在方法里, args 实际是个 String[].
  利用 for each 便可以很方便的遍历变参。
  或者通过 args.length()获取变参的实际数量。