简体   繁体   English

打印结构指针和引用的结构成员

[英]print a struct pointer and a deferenced struct member

hello all i am new to programming,i have a paradigm where i need to print a pointer of type struct which gets the value dynamically in each iteration and also print a defrenced struct member on each iteration. 你好,我是编程新手,我有一个范例,我需要打印一个struct类型的指针,它在每次迭代中动态获取值,并在每次迭代时打印一个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

}

now i need to print the values of a,x,y in the caller function, How to print those values. 现在我需要在调用函数中打印a,x,y的值,如何打印这些值。 some what like 有些什么样的

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

can someone please give me some ideas or how to proceed? 有人可以给我一些想法或如何进行? thanks alot 非常感谢

In C++, you can use std::cout : 在C ++中,您可以使用std::cout

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

Otherwise, your printf version can be fixes like this: 否则,您的printf版本可以修复如下:

printf("a=%p x=%i y=%i\n",  a, a->x, a->y);

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

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