Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

323
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda