如何将null参数传递给函数

时间:2014-10-21 16:09:46

标签: c

int val(){
   int  value;

   do{
      printf("Enter a value (between 1 & 5): ");
      scanf("%d", &value);
      if (value < 1 || value > 5){
        printf("*Error!!! Try again.\n");
      }
      else{
         printf("Thank You!\n");
      }
   } while (value < 1 || value > 5);
   return value;
}

int main(){
  int ran, value1;
  ran= (rand()% 50)+1;
  value1=val();
  return 0;
}

功能&#34; val&#34;用于验证用户的输入。 我想创建另一个函数并在main()中调用它:

void displayNum(int n, int first, int second, int third, int fourth, int fifth)

如果用户输入2,则displayNum打印&#34;首先&#34;和&#34;第二&#34;。如果用户输入5,则displayNum打印&#34; first&#34;,&#34; second&#34;,...&#34; second&#34;。

注意:&#34;第一&#34;,&#34;第二&#34;,...&#34;第五&#34;是rand()生成的数字。 &#34; int n&#34;来自&#34; val&#34;功能

3 个答案:

答案 0 :(得分:2)

void displayNum(int n, int first, int second, int third, int fourth, int fifth){
    switch(n){
        case 5:printf("%d", fifth);
        case 4:printf("%d", fourth);
        case 3:printf("%d", third);
        case 2:printf("%d", second);
        case 1:
            printf("%d", first);
            break;
        default: printf("wrong n value\n");
    }
}

答案 1 :(得分:1)

如果您想探索不同的途径,可以使用可变参数函数:

#include <stdarg.h>

void displayNum( int n, ... )
{
  va_list ap;
  va_start( ap, n );
  for ( int i = 0; i < n; i++ )
  {
    int value = va_arg( ap, int );
    printf( "%20s: %d\n", label( i+1 ), value );
  }
  va_end( ap );
  printf( "\n" );
}

其中label是一个函数,它将根据i的值打印出正确的标签。

这样您就不必传递固定数量的参数;如果您只想显示一个值,则将其称为

displayNum( 1, first );

如果要显示3个值,则将其称为

displayNum( 3, first, second, third );

第一个参数n是固定的,必须始终存在。其余参数根据第一个参数的值读取。如果传递1,displayNum将只读取并显示第一个附加参数。如果你传递3,那么displayNum将会在堆栈上有至少三个额外的整数参数。

警告:可变参数函数不是类型安全的,编译器在您传递错误类型的参数时不会发出警告,如果您传递的参数太少或太多,它也不会提醒您。如果将其称为displayNum( 1, "this is a test" );,则会导致崩溃或输出乱码。

完整示例:

#include <stdio.h>
#include <math.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>

const char *label( int n )
{
  const char *units[] = { "zeroth", "first", "second", "third", 
                          "fourth", "fifth", "sixth", "seventh", 
                          "eighth", "ninth" };
  const char *teens[] = { "tenth", "eleventh", "twelfth", "thirteenth", 
                          "fourteenth", "fifteenth", "sixteenth",
                          "seventeenth", "eighteenth", "ninteenth" };
  const char *decades[] = { "", "", "twenty", "thirty", "forty", "fifty", 
                            "sixty", "seventy", "eighty", "ninety" };

  static char buf[256] = {0};

  if ( n < 10 )
    return units[n];
  else if ( n < 20 )
    return teens[n-10];
  else
  {
    int t = n / 10;
    int u = n % 10;
    sprintf( buf, "%s", decades[t] );
    if ( u > 0 )
    {
      strcat( buf, "-" );
      strcat( buf, units[u] );
    }
    else
    {
      buf[ strlen(buf) - 1 ] = 0;
      strcat( buf, "ieth" );
    }
    return buf;
  }

  return "";
}

void displayNum( int n, ... )
{
  va_list ap;
  va_start( ap, n );
  for ( int i = 0; i < n; i++ )
  {
    int value = va_arg( ap, int );
    printf( "%20s: %d\n", label( i+1 ), value );
  }
  printf( "\n" );
  va_end( ap );
}

int main( void )
{
  displayNum( 1, 1 );
  displayNum( 2, 1, 2 );
  displayNum( 3, 1, 2, 3 );
  displayNum( 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 );
  displayNum( 25, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 
                  11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 
                  21, 22, 23, 24, 25 );
  return 0;
}

示例输出:

[fbgo448@n9dvap997]~/prototypes/stdarg: ./mystdarg
               first: 1

               first: 1
              second: 2

               first: 1
              second: 2
               third: 3

               first: 1
              second: 2
               third: 3
              fourth: 4
               fifth: 5
               sixth: 6
             seventh: 7
              eighth: 8
               ninth: 9
               tenth: 10

               first: 1
              second: 2
               third: 3
              fourth: 4
               fifth: 5
               sixth: 6
             seventh: 7
              eighth: 8
               ninth: 9
               tenth: 10
            eleventh: 11
             twelfth: 12
          thirteenth: 13
          fourteenth: 14
           fifteenth: 15
           sixteenth: 16
         seventeenth: 17
          eighteenth: 18
           ninteenth: 19
           twentieth: 20
        twenty-first: 21
       twenty-second: 22
        twenty-third: 23
       twenty-fourth: 24
        twenty-fifth: 25

如上所述,label函数只处理最多99个输入,之后你会得到一些有趣的输出。 C语言标准在单个函数调用中保证至少 127个参数,但你真的不想这样做。

修改

或者你可以避免所有这些废话,并在数组中传递你的值,如果所有的值都是相同的类型,这是更好的方法:

void displayNum( int n, const int *values )
{
  for ( int i = 0; i < n; i++ )
    printf( "%20s: %d\n", label( i + 1 ), values[i] );
}

答案 2 :(得分:0)

void displayNum(int n, int first, int second, int third, int fourth, int fifth){
     int temp=1;
     while(n>=temp){
         if(temp==5)
            printf("%d ", fifth);

         elseif(temp==4)
            printf("%d ", fourth);

         elseif(temp==3)
            printf("%d ", third);

         else if(temp==2)
            printf("%d ", second);

         else if(temp==1)
            printf("%d", first);

          temp++;

      }

}