Shankhadeep Mukerji Shankhadeep Mukerji - 1 year ago 138
C Question

how to pass struct pointer as argument in a variable argument function in C

I need to pass a struct pointer in my function which takes variable arguments. What is the correct way to do that. I tried this following code.Just pasting the code snippet, and not the full code.

struct node {
int label,
bool visited} vertex;

//My arguments in this function should be variable/unknown at runtime. It should take n number of streuct pointer arguments
int and_gate(struct vertex * v1, )
va_list valist;
va_start(valist, num) //not sure
int out=0;

for(int i=0;i<num,i++)
out = va_args(valist, v1);

int main()
struct vertex a,b,c;
and_gate(3, &a,&b,&c)

Answer Source

Use dynamic array

int and_gate(int n, struct vertex *v1)
  for(int i = 0; i < n; ++i)
    printf("%d", v1[0].label);

int main()
   int n=3;
   struct vertex *a;
   a = malloc(n * sizeof(struct vertex));
   and_gate(n, a);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download