簡體   English   中英

打印結構指針和引用的結構成員

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

你好,我是編程新手,我有一個范例,我需要打印一個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

}

現在我需要在調用函數中打印a,x,y的值,如何打印這些值。 有些什么樣的

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

有人可以給我一些想法或如何進行? 非常感謝

在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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM