简体   繁体   中英

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 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. 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 :

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

Otherwise, your printf version can be fixes like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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