0

大家好,我是编程新手,我有一个范例,我需要打印一个 struct 类型的指针,该指针在每次迭代中动态获取值,并在每次迭代时打印一个 defrended struct 成员。

struct my_struct
{
int x;
int y;
}
void function(){

my_struct* a=value.get() // some values will be assigned dynamically to this pointer from other part of function.

my_struct* data = new thread_data;
data->x=1 //which will get updated according the iterations and conditions
data->y=2 //which will get updated according the iterations and conditions

}

现在我需要在调用者函数中打印 a,x,y 的值,如何打印这些值。一些喜欢的东西

printf("a=%lx x=%i y=%i\n", a,x,y);

有人可以给我一些想法或如何进行吗?多谢

4

1 回答 1

2

在 C++ 中,您可以使用std::cout

std::cout << "a=" << a << " x=" << a->x << " y=" << a->y << "\n";

否则,您的printf版本可以像这样修复:

printf("a=%p x=%i y=%i\n",  a, a->x, a->y);
于 2013-03-19T13:40:43.863 回答