在C中打印地址或指针以获取值 [英] Print the address or pointer for value in C
问题描述
我想做一些看起来很简单的事情.我得到了结果,但是问题是,我无法知道结果是否正确.
I want to do something that seems fairly simple. I get results but the problem is, I have no way to know if the results are correct.
我正在C语言中工作,我有两个指针.我想打印指针的内容.我不想取消引用指针以获取指向的值,我只想要指针已存储的地址.
I'm working in C and I have two pointers; I want to print the contents of the pointer. I don't want to dereference the pointer to get the value pointed at, I just want the address that the pointer has stored.
我写了以下代码,我需要知道的是,如果正确,如何纠正.
I wrote the following code and what I need to know is if it is right and if not, how can I correct it.
/* item one is a parameter and it comes in as: const void* item1 */
const Emp* emp1 = (const Emp*) item1;
printf("\n comp1-> emp1 = %p; item1 = %p \n", emp1, item1 );
尽管我要发布此内容(以及正确的理由很重要),但我最终还是需要为指向指针的指针执行此操作.那就是:
While I'm posting this (and the reason it is important that it is correct) is that I eventually need to do this for a pointer-to-a-pointer. That is:
const Emp** emp1 = (const Emp**) item1;
推荐答案
您所拥有的是正确的.当然,您会看到emp1和item1具有相同的指针值.
What you have is correct. Of course, you'll see that emp1 and item1 have the same pointer value.
这篇关于在C中打印地址或指针以获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!