Sunday, 15 August 2010

stack - How can you print an item that is void** data in C? -


When trying to make a test rig for the C stack I ran into problems in a way to display it correctly. I have checked six books and they have a way to push and pop from the stack, but do not give any examples how you actually use the routine. Can someone show me the pattern, so can I write a debug print statement?

The call to pop uses this structure:

  {struct element * next; Zero * data; }   

The pop function is:

  int pop (tElement ** stack, zero ** data)   

How do I print data items?

You can print indicator value:

  zero print_data ( Zero ** data) {printf ("% p", data); If (data) printf (", *% p", * data); }   

Or, you can provide callback hooks to the user of the data structure to provide a print job for you:

  zero pretty_print_data (zero) * * Data, zero (* print_callback) (zero **)) {printf ("% p% s", data, (data? "": ":")); If (data) printf (", *% p:", * data); If (print_callback) print_callback (data); }   

The programmer providing callback knows what type of data should be :

  void print_point (zero ** Data) {if (data & amp; data) {point * p = * data; Printf ("(% d,% d)", p-> gt; x, p-> y); } Other {printf ("zero"); }}    

No comments:

Post a Comment