Как использовать аргументы функции без имени в C или C++

Как я использую аргументы функции, объявленные как

void f(double)
{
    /**/
}

если это возможно?

9
задан Potatoswatter 22 March 2010 в 08:45
поделиться

4 ответа

Надеюсь, вам поможет пример:

// Declaration, saying there is a function f accepting a double.
void f(double);

// Declaration, saying there is a function g accepting a double.
void g(double);

// ... possibly other code making use of g() ... 

// Implementation using the parameter - this is the "normal" way to use it. In
// the function the parameter is used and thus must be given a name to be able
// to reference it. This is still the same function g(double) that was declared
// above. The name of the variable is not part of the function signature.
void g(double d)
{
  // This call is possible, thanks to the declaration above, even though
  // the function definition is further down.
  f(d);
}

// Function having the f(double) signature, which does not make use of 
// its parameter. If the parameter had a name, it would give an 
// "unused variable" compiler warning.
void f(double)
{
  cout << "Not implemented yet.\n";
}
41
ответ дан 4 December 2019 в 06:11
поделиться

Параметр все еще может быть помещен в стек, поэтому вы можете найти его там (см. Комментарии ниже)

Для примера Только ( очень непереносимый )

#include<stdio.h>
void f(double)
{
    double dummy;
    printf("%lf\n",*(&dummy-2)); //offset of -2 works for *my* compiler
}

int main()
{
    f(3.0);
}

Я не уверен, зачем вам это нужно

0
ответ дан 4 December 2019 в 06:11
поделиться

Нет. Вы должны дать ему имя. Т.е.

void f(double myDouble)
{
    printf("%f", myDouble * 2);
}

или если вы используете iostreams:

void f(double myDouble)
{
    cout << myDouble * 2;
}
2
ответ дан 4 December 2019 в 06:11
поделиться

Компилятор передаст 0 по умолчанию .... это тот способ, который мы использовали для различения операторов постфиксного приращения, и нам никогда не нужно использовать фактическое переданное значение ..

-2
ответ дан 4 December 2019 в 06:11
поделиться
Другие вопросы по тегам:

Похожие вопросы: