简体   繁体   English

在 C 中使用 va_arg 搜索密钥

[英]Searching for a key using va_arg in C

The first number taken as argument is the key which is to be found in the numbers following it.作为参数的第一个数字是要在它后面的数字中找到的键。 My code ignores the first number and and stores the second number in key , and also fails to return a correct result when I add a zero at the beginning of the argument to fix choosing the second argument.我的代码忽略了第一个数字并将第二个数字存储在key中,当我在参数的开头添加一个零以修复选择第二个参数时,也无法返回正确的结果。

The code:编码:

int search(int n, ...)
{
    va_list vl;
    va_start(vl,n);
    int key,flg=0;
    key=va_arg(vl,int);
    printf("key %d\n",key);
    for(int i=2;i<=n;i++)
    {
        int x=va_arg(vl,int);
        if(key==x)
        {
            printf("FOUND!\n");
            flg=1;
            break;
        }
    }    
    if(flg==0)  
        printf("NOT FOUND!\n");
    va_end(vl);
    return 0;
}

int main(int argc,char *argv[])
{
    if(argc<=1)
    {
        printf("No arguments passed!");
    }    
    else
    {
        int key=atoi(argv[1]);
        search(0,key,34,45,12,78,11,4,90,89,54,63,7,98,43);
    } 
    return 0;
}

This code is close to your code.此代码接近您的代码。 I altered the error handling in main() — error messages should be reported on stderr , not stdout , and should end with a newline.我更改了main()中的错误处理——错误消息应该在stderr上报告,而不是在stdout上,并且应该以换行符结尾。 The main() function loops over multiple arguments so a single run can test multiple values. main() function 在多个 arguments 上循环,因此一次运行可以测试多个值。

I also changed the call so that the number of arguments in the variable list is given as n , the first argument.我还更改了调用,以便将变量列表中的 arguments 的数量作为n给出,即第一个参数。 There is no point in always returning 0 so I changed the function to return void (nothing).总是返回0是没有意义的,所以我将 function 更改为返回void (无)。 Alternatively, you should return flg so the calling code can tell whether the value was found or not.或者,您应该返回flg以便调用代码可以判断是否找到了该值。 With the function returning no value, this yields the code: function 不返回任何值,这将产生代码:

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

static void search(int n, ...)
{
    va_list vl;
    va_start(vl, n);
    int key, flg = 0;
    key = va_arg(vl, int);
    printf("key %d\n", key);
    for (int i = 0; i < n; i++)
    {
        int x = va_arg(vl, int);
        if (key == x)
        {
            printf("FOUND!\n");
            flg = 1;
            break;
        }
    }
    if (flg == 0)
        printf("NOT FOUND!\n");
    va_end(vl);
}

int main(int argc, char *argv[])
{
    if (argc <= 1)
    {
        fprintf(stderr, "Usage: %s key\n", argv[0]);
        exit(EXIT_FAILURE);
    }
    for (int i = 1; i < argc; i++)
    {
        int key = atoi(argv[i]);
        search(13, key, 34, 45, 12, 78, 11, 4, 90, 89, 54, 63, 7, 98, 43);
    }
    return 0;
}

When run (source va71.c , program va71 ), I get:运行时(源va71.c ,程序va71 ),我得到:

$ va71 89 78 34 43 199 -1
key 89
FOUND!
key 78
FOUND!
key 34
FOUND!
key 43
FOUND!
key 199
NOT FOUND!
key -1
NOT FOUND!
$

which is what I'd expect.这是我所期望的。

Here's a reorganized version which reports the position where the key is found.这是一个重组版本,它报告了找到密钥的 position。 There's no printing in the search function — keep I/O separate from the searching.搜索 function 中没有打印 — 将 I/O 与搜索分开。

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

static int search(int n, int key, ...)
{
    va_list vl;
    va_start(vl, key);
    int flg = -1;
    for (int i = 0; i < n; i++)
    {
        int x = va_arg(vl, int);
        if (key == x)
        {
            flg = i;
            break;
        }
    }
    va_end(vl);
    return flg;
}

int main(int argc, char *argv[])
{
    if (argc <= 1)
    {
        fprintf(stderr, "Usage: %s key\n", argv[0]);
        exit(EXIT_FAILURE);
    }
    for (int i = 1; i < argc; i++)
    {
        int key = atoi(argv[i]);
        printf("key %d: ", key);
        int rv = search(13, key, 34, 45, 12, 78, 11, 4, 90, 89, 54, 63, 7, 98, 43);
        if (rv < 0)
            printf("not found\n");
        else
            printf("found at index %d\n", rv);
    }
    return 0;
}

Sample output:样品 output:

$ va71 89 78 34 43 199 -1
key 89: found at index 7
key 78: found at index 3
key 34: found at index 0
key 43: found at index 12
key 199: not found
key -1: not found
$

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM