C语言 打印函数中定义的指针的值和地址?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32914298/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Print value and address of pointer defined in function?
提问by austinscode
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? Since the function is void, how can I send all three values to main?
显然这段代码只是一个骨架,但我想知道如何在函数和主要工作之间进行通信,以及打印指向的地址和 iptr 本身的语法?由于该函数是无效的,我如何将所有三个值发送到 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!
谢谢!
回答by Atahan Acar
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
回答by Barmar
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 %pformat operator requires the corresponding argument to be void*, so it's necessary to cast the pointers to this type.
该%p格式的操作需要相应的说法是void*,因此有必要对指针强制转换为这种类型。
回答by mukki
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 )
回答by Weather Vane
int* iptris 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(). It should be outside of any function, before it is called.
此外,您pointerFuncA()在错误的地方有函数原型,在里面main()。在调用之前,它应该在任何函数之外。
回答by Samer
#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 中再次重新定义函数....

