Tuesday, 15 May 2012

c - How to print the values pointed by an array of void pointers -


I have defined a structure to keep a set of values ​​of any kind, that is why zero ** I really appreciate the data if someone helps me print the price indicator from the array of zero indicators.

Here's the structure.

  typedef struct item {zero ** data; // array of zero indicator to store names of group members; Count; Member number in group structure item; // next; // pointer group for next group in pool queue}; Now, let's assume that a group is created as follows:  
  group * gp = (group *) malloc (sizeof (group)); Zero * A [2]; A [0] = (zero *) "John"; A [1] = (zero *) "gym"; GP-> Data = A; GP-> Count = 2;  

At some point, maybe inside any other function, if I need to print the array of values ​​in the group node, then what code should I write? Here's one such thing:

  zero print_group_node (group * gp) {// check for vp == zero is zero here ** VP = GP-> data; Int c = gp- & gt; Count; Int i;  

Thanks.

div>

If the data is any Type can be variable. How do you call data as data group below data_type needs another field similar to is.

  typed composition item {.... int data_type; // 0: int, 1: four * (string), ...} group;  

and use the appropriate printf () accordingly.

  Zero print_group_node (group * gp) {// Check for VP == faucet here Void = VP = GP-> Data; Int c = gp- & gt; Count; Int i; (I = 0; i & lt; c; i ++) Switch (GP-> Data_type) {Case 0: Break the printf ("% d \ n", (int *) vp [i]); Case 1: Break the break ("% s \ n", (four *) VP [ii]); Default: printf ("unknown type"); break; }}  

Currently your data needs string printf () to indicate the strings "% s" . To get rid of the warning given by <> <> Code> printf () , make typed items in VP [] that you are passing through an incompatible indicator.

  printf ("% s \ n", (four *) VP [ii])  

No comments:

Post a Comment