C language: How to pass a variable arguments list of void* to a function -


i trying pass variable arguments list of void* elements function in c.

  1. how do this?

  2. how calculate number of items in list?

  3. how loop through var-args list , pass each void* item function takes void* item parameter?

this have done, not working.

 void addvalues(list* data, void* args, ...) {   int len = sizeof (args) / sizeof (*args);    for(int i=0;i<len;i++){ processitem(args[0]); }   }   void processitem(void* item){   } 

how calculate number of items in list?

you can't. must provided or derivable.

how loop through var-args list , pass each void item function takes void* item parameter?*

as documented in variadic functions,

#include <stdarg.h>  void addvalues(int count, ...) {    va_list args;    va_start(args, count);     for(int i=count; i--; )       processitem(va_arg(args, void*));     va_end(args); } 

example usage:

void* p1 = ...; void* p2 = ...; void* p3 = ...; void* p4 = ...;  addvalues(4, p1, p2, p3, p4); 

it depends on doing, should using array instead of variadic parameters.

void addvalues(int count, const void** args) {    for(int i=count; i--; )       processitem(*(args++)); } 

example usage:

#define c_array_length(a) (sizeof(a)/sizeof((a)[0]))  void* ptrs[4]; ptrs[0] = ...; ptrs[1] = ...; ptrs[2] = ...; ptrs[3] = ...;  addvalues(c_array_len(ptrs), ptrs); 

or (if pointers can't null):

void addvalues(const void** args) {    while (*args != null)       processitem(*(args++)); } 

example usage:

void* ptrs[5]; ptrs[0] = ...; ptrs[1] = ...; ptrs[2] = ...; ptrs[3] = ...; ptrs[4] = null;  addvalues(ptrs); 

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -