2013-03-19 66 views
0

你好我都是新手编程,我有一个范例,我需要打印一个结构类型的指针,它在每次迭代中动态获取值,并打印一个defrenced结构成员每次迭代。打印一个结构指针和一个推定的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); 

有人可以请给我一些想法或如何进行?非常感谢

+2

使用'%p'打印指针。检查'man 3 printf'。 – 2013-03-19 13:39:42

回答

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); 
+0

在OP的代码中看到'new'和'.get()',可能不需要提及'printf()'。 – Angew 2013-03-19 13:44:31

+0

@Angew''printf'直接来自OP的代码。我试图修复他们的版本。 – juanchopanza 2013-03-19 13:45:51

+0

'std :: cout <<“a =”<< a <<“x =”<< a-> x <<“y =”<< a-> y <<“\ n”;'只给a就会给出存储在' my_struct * a'?或存储在'my_struct * a'中的地址? – Rd7 2013-03-19 13:48:16