指针在 printf() 中不起作用

发布于 2024-10-26 06:58:18 字数 476 浏览 1 评论 0原文

打印指针时出现问题。每次我尝试编译下面的程序时,都会出现以下错误:

pointers.c:11: warning: format ‘%p’ expects type ‘void *’, but argument 2 has type ‘int *’

我显然在这里缺少一些简单的东西,但从我见过的类似代码的其他示例来看,这应该可以工作。

这是代码,任何帮助都会很棒!

#include <stdio.h>

    int main(void)
    {
       int x = 99;
       int *pt1;

       pt1 = &x;

       printf("Value at p1: %d\n", *pt1);
       printf("Address of p1: %p\n", pt1);

       return 0;
    }

Having an issue with printing a pointer out. Every time I try and compile the program below i get the following error:

pointers.c:11: warning: format ‘%p’ expects type ‘void *’, but argument 2 has type ‘int *’

I'm obviously missing something simple here, but from other examles of similar code that I have seen, this should be working.

Here's the code, any help would be great!

#include <stdio.h>

    int main(void)
    {
       int x = 99;
       int *pt1;

       pt1 = &x;

       printf("Value at p1: %d\n", *pt1);
       printf("Address of p1: %p\n", pt1);

       return 0;
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

弥枳 2024-11-02 06:58:19

只需将 int 指针转换为 void 即可:

printf( "Address of p1: %p\n", ( void * )pt1 );

您的代码是安全的,但您正在使用 -Wformat 警告标志进行编译,该标志将检查对 printf() 的调用和scanf()。

Simply cast your int pointer to a void one:

printf( "Address of p1: %p\n", ( void * )pt1 );

Your code is safe, but you are compiling with the -Wformat warning flag, that will type check the calls to printf() and scanf().

秋凉 2024-11-02 06:58:19

请注意,您会收到一个简单的警告。您的代码可能将按预期执行。

printf 的 "%p" 转换说明符需要一个 void* 参数; pt1 的类型为 int*

这个警告是好的,因为 int*void* 在奇怪的实现中可能具有不同的大小或位模式或其他内容

通过强制转换将 int* 转换为 void* ...

printf("%p\n", (void*)pt1);

... 一切都会好的,即使是在奇怪的实现上。

Note that you get a simple warning. Your code will probably execute as expected.

The "%p" conversion specifier to printf expects a void* argument; pt1 is of type int*.

The warning is good because int* and void* may, on strange implementations, have different sizes or bit patterns or something.

Convert the int* to a void* with a cast ...

printf("%p\n", (void*)pt1);

... and all will be good, even on strange implementations.

后eg是否自 2024-11-02 06:58:19

在这种情况下,编译器对警告有点过于渴望了。您的代码是完全安全的,您可以选择通过以下方式删除警告:

printf("Address of p1: %p\n", (void *) pt1);

In this case, the compiler is just a bit overeager with the warnings. Your code is perfectly safe, you can optionally remove the warning with:

printf("Address of p1: %p\n", (void *) pt1);
把回忆走一遍 2024-11-02 06:58:19

该消息说明了一切,但这只是一个警告,而不是错误本身:

printf("Address of p1: %p\n", (void*)pt1);

The message says it all, but it's just a warning not an error per se:

printf("Address of p1: %p\n", (void*)pt1);
梦毁影碎の 2024-11-02 06:58:19

这对我来说效果很好:

printf("Pointer address: %p.", pxy);

你不需要将它转换成任何东西,除非你想......

This worked just fine for me:

printf("Pointer address: %p.", pxy);

You don't need to cast it as anything, unless you wanted to...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文