简体   繁体   English

打印函数中定义的指针的值和地址?

[英]Print value and address of pointer defined in function?

I think this is a really easy thing to code, but I'm having trouble with the syntax in C, I've just programmed in C++.我认为这是一个非常容易编码的事情,但是我在 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;
}

Obviously this code is just a skeleton but I'm wondering how I can get the communication between the function and the main working, and the syntax for printing the address pointed to and of iptr itself?显然这段代码只是一个骨架,但我想知道如何在函数和主要工作之间进行通信,以及打印指向的地址和 iptr 本身的语法? Since the function is void, how can I send all three values to main?由于该函数是无效的,我如何将所有三个值发送到 main?

I think the address is something like:我认为地址是这样的:

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

I know it's a simple question, but all the examples I found online just got the value, but it was defined in main as something like我知道这是一个简单的问题,但我在网上找到的所有示例都得到了值,但它在 main 中被定义为类似

int iptr = 0;

Would I need to create some arbitrary value?我需要创建一些任意值吗?

Thanks!谢谢!

Read the comments阅读评论

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

Output:输出:

Value:  1234
Value:  0xffe2ac6c
Value:  0xffe2ac44

To access the value that a pointer points to, you have to use the indirection operator * .要访问指针指向的值,您必须使用间接运算符*

To print the pointer itself, just access the pointer variable with no operator.要打印指针本身,只需访问指针变量而不使用运算符。

And to get the address of the pointer variable, use the & operator.要获取指针变量的地址,请使用&运算符。

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

The %p format operator requires the corresponding argument to be void* , so it's necessary to cast the pointers to this type. %p格式运算符要求相应的参数为void* ,因此有必要将指针强制转换为该类型。

int* iptr is already a pointer, so you don't need the & in front of it when you write int* iptr已经是一个指针了,所以写的时候不需要在它前面加&

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

This is how to print a pointer value.这是打印指针值的方法。

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

Also you have the function prototype for pointerFuncA() in the wrong place, being inside main() .此外,您将pointerFuncA()的函数原型pointerFuncA()错误的位置,即在main() It should be outside of any function, before it is called.在调用之前,它应该在任何函数之外。

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

I think you are looking at something like this, there is no need to re-define the function again in the main....我想你正在看这样的东西,没有必要在 main 中再次重新定义函数....

Address are some memory values which are written in hexadecimal notation starting with 0x地址是一些以 0x 开头的十六进制表示法写入的内存值

/ Value pointed to by the pointer iptr / /指针 iptr 指向的值/

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

Address pointed to by the pointer will be the value of the iptr pointer itself指针指向的地址将是 iptr 指针本身的值

/ print the address pointed to by the iptr / /打印 iptr 指向的地址/

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

/ print the address of iptr itself / /打印 iptr 本身的地址/

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

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

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