C语言 C 警告函数调用中缺少哨兵

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2407605/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 04:47:03  来源:igfitidea点击:

C warning Missing sentinel in function call

cwarnings

提问by ambika

This is my warning.

这是我的警告。

Missing sentinel in function call

How i can remove it.

我怎样才能删除它。

i am using linux & gcc compiler.

我正在使用 linux 和 gcc 编译器。

回答by Michael Gaylord

It looks like you may not have terminated an array declaration with NULL. Without the null you may have some memory weirdness as the runtime won't know where the array ends and the next bit of memory starts.

看起来您可能没有使用NULL. 如果没有空值,您可能会有一些内存怪异,因为运行时将不知道数组在哪里结束以及内存的下一位开始。

回答by Peter Bagnall

I just came across the same issue. The code which was causing it for me was...

我刚刚遇到了同样的问题。对我造成它的代码是......

execl("/bin/bash", "/bin/bash", fname, '
execl("/bin/bash", "/bin/bash", fname, (char *)0);
');

but it should be...

但应该是...

execl("/bin/bash", "/bin/bash", fname, NULL);

The problem with the first version is that the list of parameters is meant to end with a null pointer. But '\0' is not a null pointer, it's a null character. So the value (0) is correct, it's just the type is wrong.

第一个版本的问题是参数列表以空指针结尾。但是 '\0' 不是空指针,而是空字符。所以值 (0) 是正确的,只是类型错误。

The (char *)0 is also zero, but cast as a char pointer, which is a null pointer (ie it points to address 0). This is needed so the system can tell where the parameter list ends so that it doesn't keep scanning for parameters after the last one. Doing that would get it invalid pointers which could point to any memory - which likely would cause a segmentation fault.

(char *)0 也是零,但转换为 char指针,它是一个空指针(即它指向地址 0)。这是必要的,这样系统就可以知道参数列表在哪里结束,这样它就不会在最后一个参数之后继续扫描参数。这样做会得到可能指向任何内存的无效指针 - 这可能会导致分段错误。

That (char *)0 is called the sentinel, and it's what was missing from the first example.

那个 (char *)0 被称为哨兵,这是第一个例子中所缺少的。

Finally note that NULL is defined as (void *)0, so

最后注意NULL被定义为(void *)0,所以

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

void print_strings(const char* first, ...) __attribute__((sentinel));

void print_strings(const char* first, ...)
{
    va_list ap;
    const char* tmp;
    if (!first)
        return ;
    printf("%s\n", first);
    va_start(ap, first);
    while (1) {
        tmp = va_arg(ap, const char*);
        if (tmp == 0)
            break;
        printf("%s\n", tmp);
    };
    va_end(ap);
}

int main()
{
    print_strings("how are you?", "i'm fine", "and you?", NULL);
    return 0;
}

Works just as well, and is a little more convenient. (Thanks to @mah for that).

工作也一样,而且更方便一些。(感谢@mah)。

回答by kslcam

In Xcode if you are coding in objective-c and you are using some methods which take variable parameter list, you need to add nilobject at the end of the list.

在 Xcode 中,如果你在 Objective-c 中编码并且你正在使用一些带有可变参数列表的方法,你需要在列表的末尾添加nil对象。

For example:

例如:

NSArray *names = [NSArray arrayWithObjects: @"Name1", @"Name2"];//will result in the warning mentioned above

N SArray *names = [NSArray arrayWithObjects: @"Name1", @"Name2"]; //会导致上面提到的警告

However, NSArray *names = [NSArray arrayWithObjects: @"Name1", @"Name2", nil]; //Correct

然而, NSArray *names = [NSArray arrayWithObjects: @"Name1", @"Name2", nil]; //正确的

Hope this will help!

希望这会有所帮助!

回答by Jichao

A trivial example:

一个简单的例子:

[NSDictionary dictionaryWithObjectsAndKeys:@"UIAlertView", kSectionTitleKey,
              @"Show Custom", kLabelKey,
              @"AlertsViewController.m - alertOtherAction", kSourceKey];

In main, if you call print_strings as print_strings("how are you?", "I'm fine", "and you?")which has no ending NULL, GCC will complain "missing sentinel".

在 main 中,如果你调用 print_strings as print_strings("how are you?", "I'm fine", "and you?")which has no end NULL,GCC 会抱怨“缺少哨兵”。

Because we add the sentinel function attribute to the function print_strings. It is a gcc extension to specify that the variable arguments should end with NULL. So if you do not end variable arguments with NULL, the compiler could detect it and show the warning.

因为我们给函数添加了哨兵函数属性print_strings。它是一个 gcc 扩展,用于指定变量参数应以 NULL 结尾。因此,如果您不以 NULL 结束变量参数,编译器可以检测到它并显示警告。

回答by Sanidhya Srivastava

You can pass NULL as: execl("/bin/bash", "ls","-l", NULL); The last parameter must always be 0. It is a NULL terminator. Since the argument list is variable we must have some way of telling C when it is to end.

您可以将 NULL 传递为: execl("/bin/bash", "ls","-l", NULL); 最后一个参数必须始终为 0。它是一个NULL 终止符。由于参数列表是可变的,我们必须有某种方式告诉 C 何时结束。

回答by taus-iDeveloper

Sentinel means to guard or protect.. So in this context the error is occuring cause you might be missng the guarding parameters. In case you are using an Array or Dictionary then make sure that after the naming the objects you end them with the keyword nil.

Sentinel 的意思是保护或保护..所以在这种情况下,错误正在发生,因为您可能会错过保护参数。如果您使用的是数组或字典,请确保在命名对象后以关键字 nil 结束它们。

Example:

例子:

[NSDictionary dictionaryWithObjectsAndKeys:@"UIAlertView", kSectionTitleKey,
              @"Show Custom", kLabelKey,
              @"AlertsViewController.m - alertOtherAction",kSourceKey,nil];

The above statement will produce an error "Missing sentinel in function call"

上面的语句会产生错误“Missing sentinel in function call”

Correct syntax:

正确的语法:

UIAlertView* alert = [ [ UIAlertView alloc ] initWithTitle: @"Title" message: @"Message" delegate: nil cancelButtonTitle: @"Cancel" otherButtonTitles: @"OK", ( NSString* )nil ];

回答by Kyrylo Polezhaiev

You should pass NULLas last argument of function.

您应该NULL作为函数的最后一个参数传递。

回答by medoteddy

I finally found a way to get rid of this strange and annoying warning.

我终于找到了摆脱这个奇怪而烦人的警告的方法。

All you need to do is to cast a nil pointer to an appropriate pointer type.

您需要做的就是将 nil 指针转换为适当的指针类型。

In case of UIAlertViewit goes like this:

如果UIAlertView它是这样的:

##代码##

Notice the ( NSString* )nilcast.

注意( NSString* )nil演员阵容。

Have a try and let me know if this works for you.

试一试,让我知道这是否适合你。