c语言全局变量作为参数_在C / C ++中使用变量参数列表

c语言全局变量作为参数

C/C++ provides a means to pass a variable number of arguments to a function.  This article shows how to use that to your advantage, and also discusses the potential problems that your program might encounter if you do so.

C / C ++提供了一种将可变数量的参数传递给函数的方法。 本文介绍了如何利用它来发挥自己的优势,并讨论了如果这样做可能会导致程序遇到的潜在问题。

Coming from an ASM background and knowing how parameters are passed to function calls, this feature of C totally amazed me when I first saw it -- over thirty years ago.  How on earth can a compiler know what program code to generate when there is no set number of arguments?

出于ASM的背景,并且知道如何将参数传递给函数调用,当我30年前第一次看到它时,C的这一功能令我非常惊讶。

The first, and probably most significant, piece of the puzzle is that in C/C++, the calling function is responsible for fixing the stack after a call; that is, the compiler automatically generates code to do that.  This is different from Pascal and some other language conventions.  It means that however many arguments are pushed onto the stack before the call, they are automatically removed from the stack after the call.  

第一个,也是最重要的难题是,在C / C ++中,调用函数负责在调用后修复堆栈。 也就是说,编译器会自动生成代码来执行此操作。 这与Pascal和其他一些语言约定不同。 这意味着无论调用前有多少参数被压入堆栈,调用后它们都会自动从堆栈中删除。

The calling function easily knows how much stack space was used.  But how can the called function know?  When using a Variable Argument List, you need to provide some sort of mechanism so that the called function knows how many function arguments to process.

调用函数很容易知道已使用了多少堆栈空间。 但是被

Embedded, Interpreted at Run-time

嵌入式,在运行时解释

You are certainly familiar with the most well-known example of this type of function:  printf (and sprintf, etc.) It knows how many arguments were passed because the first (required) string parameter contains some number of formating specifiers embedded in the string.  For instance, if it contains:

您肯定熟悉这种函数的最著名的示例: printf (和sprintf等),因为第一个(必需)字符串参数包含一些嵌入在字符串中的

  "My name is %s.  I am %d years old."

“我叫%s 。我%d岁。”

... then printf knows that there will be a string pointer ("%s") and an integer ("%d") on the stack, in that order.

...然后printf知道堆栈上将有一个字符串指针(“%s”)和一个整数(“%d”)以此顺序排列。

So, one mechanism is that the caller passes a string that can be interpreted at run-time to determine how many (if any) extra arguments are on the stack.

因此,一种机制是调用者传递一个可以在运行时解释的字符串,以确定堆栈上有多少个(如果有)额外参数。

Examine Arguments for Specific Value

检查参数的特定值

Another mechanism that might be used is to cycle through the arguments until you hit a specific value.  For instance, a function that sums up all of the positive integer values passed to it might stop when it hits a value of -1.  A function that concatenates a variable number of string arguments might stop when it hits NULL or "".

可能使用的另一种机制是循环遍历参数,直到达到特定值为止。 例如,一个对所有传递给它的正整数求和的函数,当它达到-1时可能会停止。 当字符串为NULL或“”时,连接可变数目的字符串参数的函数可能会停止。

One Argument Specifically Indicates How Many Other Arguments There Are

一个参数专门表示有多少

Another mechanism is more straight-forward:  The calling function is required to pass an integer value as one of the early, required parameters.

另一种机制更简单:调用函数需要传递一个整数值作为早期的

We'll look at examples of these and get into the specifics of how to "walk the argument list."  But first, let's look at an example that you can use without needing to understand the underlying mechanism.

我们将看看这些示例,并详细介绍如何“遍历参数列表”。 但首先,让我们看一个无需了解底层机制即可使用的示例。

Var Arg List as a Black Box

Var Arg列表为黑匣子

The ATL/MFC CString data type provides a Format() function that gives you a prin

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值