gpt4 book ai didi

c - 指针在 printf() 中不起作用

转载 作者:太空宇宙 更新时间:2023-11-04 00:00:48 25 4
gpt4 key购买 nike

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

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;
}

最佳答案

只需将您的 int 指针转换为 void 指针:

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

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

关于c - 指针在 printf() 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45657210/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com