Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

324
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda