Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

318
Views
How to pass literal array as input argument of the function?

The goal is to pass a constant array, (representing the member variables of the corresponding structure parameter) like {{"network", "lan"}, {"dhcp", "true"}} as parameter when calling a function like:

ubus_call("router", "client", {{"network", "lan"}, {"dhcp", "true"}}, 2);

I tried the following code but it returns errors in the compilation:

struct ubus_args {
 char *key;
 char *val;
};

int ubus_call(char *obj, char *method, struct ubus_args u_args[], int size_args) {
 printf("%s\n", obj);
 printf("%s\n", method);
 printf("%s  %s\n", u_args->key, u_args->val);
 return 0;
}

int main ()
{
  ubus_call("router", "client", {{"network", "lan"}, {"dhcp", "true"}}, 2);
  return 0;
}

How I can do that in the proper way?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Here is the complete program, you can try it out.

#include <stdio.h>

struct ubus_args {
    char *key;
    char *val;
};

int ubus_call(char *obj, char *method, struct ubus_args u_args[], int size_args) {
    printf("%s\n", obj);
    printf("%s\n", method);
    printf("%s  %s\n", u_args[0].key, u_args[0].val);
    printf("%s  %s\n", u_args[1].key, u_args[1].val);
    return 0;
}

int main ()
{
    ubus_call("router", "client", (struct ubus_args[2]){{"network", "lan"}, {"dhcp", "true"}}, 2);
    return 0;
}

The program is tested on GNU GCC v4.8.3 online compiler.

over 4 years ago · Santiago Trujillo Report

0

If you're equipped with a C99 and above supported compiler, you can use compound literals to get your job done.

You can rewrite the function call like

ubus_call("router", "client", (struct ubus_args[]){{"network", "lan"}, {"dhcp", "true"}}, 2);

and it will work.

LIVE DEMO

BTW, the recommended signature of main() is int main(void).

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!