指针不会工作的printf() [英] Pointer will not work in printf()

查看:95
本文介绍了指针不会工作的printf()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个问题与打印指针移出。每次我尝试和编译程序下面我得到以下错误:

  pointers.c:11:警告:格式'%P'需要键入'无效*',但参数2的类型'诠释*'

我显然简单的东西在这里,但我见过类似code等examles,这应该是工作。

这里的code,任何帮助将是巨大的!

 的#include<&stdio.h中GT;    INT主要(无效)
    {
       INT X = 99;
       INT * PT1;       PT1 =安培; X;       的printf(价值在P1数:%d \\ n,* PT1);
       的printf(P1地址:%P \\ N,PT1);       返回0;
    }


解决方案

只需将您的INT指针转换为void之一:

 的printf(P1地址:%P \\ N(无效*)PT1);

您code是安全的,但你是用 -Wformat 警告标志进行编译,将键入检查的printf的调用( ) scanf()的

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

解决方案

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().

这篇关于指针不会工作的printf()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆