简体   繁体   English

C 中的指针和转换

[英]Pointers in C and casting

I've started learning pointers this time.这次我开始学习指针了。 Im trying to read bytes from this array.我试图从这个数组中读取字节。 Task is almost done but CLang keep warns me with warning I don't understand.任务快完成了,但 CLang 一直警告我,警告我不明白。 Here's my code.这是我的代码。 Warning says : " Function call argument is an uninitialized value"警告说:“函数调用参数是一个未初始化的值”

int main(void)
{
    int tab[] = {67305985,134678021,202050057};
    int *pp=0;
    pp=tab;

    char *wsk=(char*)pp;

    for (int i = 0; i < 12; i++)
    {
        if((wsk+i)!=(void*)NULL)
            printf("%d ",*(wsk+i));    // warning on this line
        else
            return 0;
    }

}

These warnings are from Clang Static Analyzer (or whatever they call it these days).这些警告来自 Clang 静态分析器(或者现在他们称之为什么)。

It looks like a false positive to me, if we assume that int is at least 4 bytes and the real code has #include <stdio.h> and no other changes.如果我们假设int至少为 4 个字节并且实际代码具有#include <stdio.h>并且没有其他更改,那么对我来说这看起来像是误报。

If you're using the latest version of the analyzer, you could file a clang bug report.如果您使用的是最新版本的分析器,则可以提交 clang 错误报告。 Well -- you could if they allowed people who don't already have accounts to file bug reports.好吧 - 如果他们允许还没有帐户的人提交错误报告,则可以。 Maybe someone else reading this thread can do it.也许阅读此线程的其他人可以做到。

Note: it would help the question to post exactly which version of the analyzer you are running (this may be different to the compiler you're using to build -- some IDEs use different compilers for building than for these inline messages).注意:准确地发布您正在运行的分析器版本(这可能与您用来构建的编译器不同——某些 IDE 使用不同的编译器进行构建而不是这些内联消息),这将有助于解决这个问题。

This if statement这个 if 语句

if((wsk+i)!=(void*)NULL)

does not make sense.没有意义。 The macro NULL is already a null pointer constant.NULL已经是一个空指针常量。 So there is no sense to cast it to void * .因此将其void *void *是没有意义的。

And the pointer wsk+i can not be equal to NULL in this loop because initially it points to an object.在这个循环中,指针wsk+i不能等于 NULL,因为它最初指向一个对象。

Just remove the if statement and output each character in the loop.只需删除 if 语句并输出循环中的每个字符。

And it is a bad idea to use magic numbers like 12 used in the loop.在循环中使用像 12 这样的幻数是一个坏主意。

You could write for example你可以写例如

const size_t N = sizeof( tab ) / sizeof( *tab );

and then in the loop然后在循环中

for ( size_t i = 0; i < N * sizeof( int ); i++ )
//...

As for the warning then it is irrelative to the presented code provided that you included the header <stdio.h> .至于警告,那么它与提供的代码无关,前提是您包含了标题<stdio.h>

wsk will never be null, since you've set it to be the same as tab (arrays, in C/C++ are just pointers to a block of data. The warning is telling you that no matter how much you add to wsk it will always be non-null. What you need to do is limit your iterations in the loop to the number of valid items in the array, since C/C++ arrays have no terminators of any kind. wsk永远不会为空,因为您已将其设置为与tab相同(数组,在 C/C++ 中只是指向数据块的指针。警告告诉您,无论您向wsk添加wsk它都会始终为非空。您需要做的是将循环中的迭代限制为数组中有效项的数量,因为 C/C++ 数组没有任何类型的终止符。

The printf might just be confusing CLang. printf 可能只是混淆了 CLang。 Try printf("%d ", wsk[i]);试试printf("%d ", wsk[i]);

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

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