【问题标题】:Parameter passing in C functions [duplicate]C函数中的参数传递[重复]
【发布时间】:2015-02-27 16:00:09
【问题描述】:

我是新手。所以请多多包涵,

#include<stdio.h>

    int abc(int k)
    {
    k++;

    }
    int main()
    {
    int a=1;
    printf("%d",abc(a));

    return 0;
    }

上述程序的输出是:1

我的问题是输出不应该是“2”,因为实际参数将“1”的值传递给形式参数,并且它必须由函数 abc 递增。

当我将函数调用更改为

printf("%d",abc(1));

输出是一些垃圾值。

参数传递在这里是如何工作的?请解释一下。

【问题讨论】:

  • 总是在编译时启用警告-Wall -Wextra 至少。这会泄露warning: control reaches end of non-void function 告诉你你没有从abc 返回任何东西。
  • 你的程序错了,因为函数abc应该返回一个int,但它永远不会返回。因此,您的程序不会崩溃只是偶然的。为避免将来出现这种情况,请使用 -Wall 标志 (gcc/clang) 或在编译器选项 (Visual Studio) 中启用编译器警告。它会说诸如“警告:某些代码路径不返回值”之类的内容,这将使您提前意识到自己的错误。
  • @DavidC.Rankin 我认为-Wextra 对于初学者来说可能太烦人了。当你使用-Wextra 时,有时需要样板来获得干净的编译(想想未使用的参数)
  • @GiulioFranco,这取决于。在少数情况下,-Wextra 需要的不仅仅是确保您的比较是正确的。请参阅:-Wextra how useful is it really? 它的作用是迫使您 (1) 理解编译器告诉您的内容和 (2) 编写正确的代码。无论是初学者还是专家,当编译器能够识别出问题所在并为您提供足够描述性的警告以指出您正确的修复方向时,草率的代码就没有任何借口了。
  • @DavidC.Rankin 如果您是专家,您可能会根据每个警告决定禁用哪些警告。如果您是新手,您只是让我相信最好启用所有功能。

标签: c function parameter-passing


【解决方案1】:

您得到的意外结果不是“参数传递”导致的,而是abc 函数没有返回任何值的事实。您应该使用return k; 语句来获得您期望的输出。但是对于参数传递,它们是按值传递的,即传递的值被复制到一个临时位置k(仅在函数中可见),而不是在它之外修改。

【讨论】:

  • ... 编译器应该对此发出警告。
  • 它实际上确实返回了——不管此时堆栈中发生了什么,但仍然如此。
  • @JensGustedt 以哪种方式?对我来说似乎很好,虽然很简洁。
  • 无论如何,经过编辑以反映所有方面。
  • 但是当我通过 abc(1) .. 它正在打印一些垃圾值。为什么打印垃圾值?
【解决方案2】:

您的代码示例通过a 按值。您可以将其视为a副本。你用 cmets 修改了代码:

#include<stdio.h>

int abc(int k)
{
    // k is a copy of a, it is not a, since k is a copy, it has the 
    // value of a at the point of the copy.  So, k is 1

    k++;  // k is now 2
    return k; // return the computed value to the caller and destroy k
}

int main()
{
    int a=1;

    // as previously written, without the return statement in abc()
    // this function returned nothing.  So, the compiler just arranges
    // for something to be used from the stack where the return would 
    // have placed 2.  (I'm not terribly familiar
    // with assembly and so I'm not sure which register it would use).
    // That's why you get non-nonsensical data, whatever is in memory is
    // what you get and without the return statement, there's nothing 
    // meaningful there.

    // Also, as I commented above, abc() takes a **copy** of a.  Thus,
    // the contents of a are unmodified.  See how the printf() is
    // changed.  What does it print? 
    printf("%d %d",abc(a), a);

    return 0;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-07
    • 2015-12-13
    • 1970-01-01
    • 2014-11-07
    • 2017-02-12
    • 2017-07-05
    相关资源
    最近更新 更多