【问题标题】:Print value and address of pointer defined in function?打印函数中定义的指针的值和地址?
【发布时间】:2015-12-31 02:02:08
【问题描述】:

我认为这是一件非常容易编码的事情,但我在 C 中的语法上遇到了问题,我刚刚用 C++ 进行了编程。

#include <stdio.h>
#include <stdlib.h>

void pointerFuncA(int* iptr){
/*Print the value pointed to by iptr*/
printf("Value:  %x\n", &iptr );

/*Print the address pointed to by iptr*/

/*Print the address of iptr itself*/
}

int main(){

void pointerFuncA(int* iptr); 

return 0;
}

显然,这段代码只是一个框架,但我想知道如何获得函数和主工作之间的通信,以及打印指向的地址和 iptr 本身的语法?由于该函数是无效的,我怎样才能将所有三个值都发送到 main?

我认为地址类似于:

printf("Address of iptr variable: %x\n", &iptr );

我知道这是一个简单的问题,但是我在网上找到的所有示例都得到了价值,但它在 main 中被定义为类似

int iptr = 0;

我需要创建一些任意值吗?

谢谢!

【问题讨论】:

    标签: c pointers


    【解决方案1】:

    阅读cmets

    #include <stdio.h>
    #include <stdlib.h>
        
    void pointerFuncA(int* iptr){
      /*Print the value pointed to by iptr*/
      printf("Value:  %d\n", *iptr );
        
      /*Print the address pointed to by iptr*/
      printf("Value:  %p\n", iptr );
    
      /*Print the address of iptr itself*/
      printf("Value:  %p\n", &iptr );
    }
        
    int main(){
      int i = 1234; //Create a variable to get the address of
      int* foo = &i; //Get the address of the variable named i and pass it to the integer pointer named foo
      pointerFuncA(foo); //Pass foo to the function. See I removed void here because we are not declaring a function, but calling it.
       
      return 0;
    }
    

    输出:

    Value:  1234
    Value:  0xffe2ac6c
    Value:  0xffe2ac44
    

    【讨论】:

    • @chux "%x" 为十六进制,可用于显示指针。唯一的区别是开头的 0x。
    • C 规范不支持 ""%x" ... 可用于显示指针"。 “o,u,x,X unsigned int 参数已转换...”和“如果转换规范无效,则行为未定义。”
    • 指针大小可能与 int 大小不同。
    • "%p" 用于void *。尽管int *void * 具有相同的大小和其他属性很常见,但C 没有指定。对于可移植的答案,任何可以转换为void * 的指针首先被转换为void *,如stackoverflow.com/a/32914514/2410359.
    【解决方案2】:

    要访问指针指向的值,您必须使用间接运算符*

    要打印指针本身,只需访问不带运算符的指针变量。

    要获取指针变量的地址,请使用&amp; 运算符。

    void pointerFuncA(int* iptr){
        /*Print the value pointed to by iptr*/
        printf("Value:  %x\n", *iptr );
    
        /*Print the address pointed to by iptr*/
        printf("Address of value: %p\n", (void*)iptr);
    
        /*Print the address of iptr itself*/
        printf("Address of iptr: %p\n", (void*)&iptr);
    }
    

    %p 格式运算符要求对应的参数为void*,因此需要将指针强制转换为该类型。

    【讨论】:

    • "%p" 匹配 void *。大多数系统的行为与"%p"int * 一样,但最好转换为void *。 “p 参数应该是指向 void 的指针。” C11dr §7.21.6.1 8
    • @chux ... 和(void**) 在第三种情况下??
    【解决方案3】:

    地址是一些以0x开头的十六进制表示的内存值

    /指针iptr指向的值/

    printf("Value is: %i", *iptr);
    

    指针指向的地址就是iptr指针本身的值

    /打印iptr指向的地址/

     printf("Address is: %p", iprt);
    

    /打印iptr本身的地址/

     printf("Address of iptr: %p", &iptr )
    

    【讨论】:

      【解决方案4】:

      int* iptr已经是一个指针了,写的时候不需要前面的&amp;

      printf("Address of iptr variable: %x\n", &iptr );
      

      这是打印指针值的方法。

      printf("Address of iptr variable: %p\n", (void*)iptr);
      

      你还有 pointerFuncA() 的函数原型在错误的地方,在 main() 里面。它应该在任何函数之外,在它被调用之前。

      【讨论】:

      • 这是iptr指向的地址,不是iptr本身的地址。
      • @Barmar 同意,仔细阅读,OP 询问 “打印指向的地址和 iptr 本身的语法”
      • 查看代码中的cmets。他想打印 3 种不同的东西。
      • 是的,打印三个不同的值/地址。感谢大家的帮助!
      【解决方案5】:
      #include <stdio.h>
      #include <stdlib.h>
      
      void pointerFuncA(int* iptr){
      /*Print the value pointed to by iptr*/
      printf("Value:  %p\n", (void*) iptr );
      
      /*Print the address pointed to by iptr*/
      
      /*Print the address of iptr itself*/
      }
      
      int main(){
      int iptr = 0;
      pointerFuncA( &iptr); 
      
      return 0;
      }
      

      我觉得你看的是这样的东西,没必要在main里重新定义函数了....

      【讨论】:

      • 好的,这对价值有效。有没有办法打印指向的地址和iptr的地址?我还是只传入 iptr,所以不改变 main 吗?
      • 在这段代码中你打印的是iptr的地址,地址指向是什么意思?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-11-06
      • 2021-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-07
      相关资源
      最近更新 更多