首页 > 解决方案 > 使用 argv 和 argc 向函数发送参数

问题描述

有人可以帮助我了解我需要如何将参数发送到函数“lora_rf_config”吗?非常感谢 !

我尝试:

char cfgred[7][10]={'lora_rf_config','915000000','10','0','1','8','14'};
lora_rf_config(7,&cfgred);

我试图使用的功能是:

static void lora_rf_config(int argc, char *argv[])
{
    if (argc == 1) {      
        e_printf("OK%d,%d,%d,%d,%d,%d\r\n",  g_lora_config.lorap2p_param.Frequency, 
                                             g_lora_config.lorap2p_param.Spreadfact,
                                             g_lora_config.lorap2p_param.Bandwidth,
                                             g_lora_config.lorap2p_param.Codingrate,
                                             g_lora_config.lorap2p_param.Preamlen,
                                             g_lora_config.lorap2p_param.Powerdbm );
        return;
    } else {
        if (argc != 7) {
            out_error(RAK_ARG_ERR);
            return;
        }
    if (!(CHECK_P2P_FREQ(atoi(argv[1])) &&
             CHECK_P2P_SF(atoi(argv[2])) &&
             CHECK_P2P_BDW(atoi(argv[3])) &&
             CHECK_P2P_CR(atoi(argv[4])) &&
             CHECK_P2P_PREMLEN(atoi(argv[5])) &&
             CHECK_P2P_PWR(atoi(argv[6])))) {
            out_error(RAK_ARG_ERR);
        return;  
      } 

      if (read_partition(PARTITION_0, (char *)&g_lora_config, sizeof(g_lora_config)) < 0) {
          out_error(RAK_RD_CFG_ERR);
          return;
      }

      g_lora_config.lorap2p_param.Frequency = atoi(argv[1]);
      g_lora_config.lorap2p_param.Spreadfact = atoi(argv[2]);
      g_lora_config.lorap2p_param.Bandwidth = atoi(argv[3]);
      g_lora_config.lorap2p_param.Codingrate = atoi(argv[4]);
      g_lora_config.lorap2p_param.Preamlen = atoi(argv[5]);
      g_lora_config.lorap2p_param.Powerdbm = atoi(argv[6]);
      write_partition(PARTITION_0, (char *)&g_lora_config, sizeof(g_lora_config));
      e_printf("OK\r\n");
    }

    return;
}

我得到的错误是:

..\..\..\src\application\RAK811\app.c(107): error:  #26: too many characters in character constant
  char cfgred[7][10]={'lora_rf_config','915000000','10','0','1','8','14'};

我对这种论点没有经验。感谢您的时间。

标签: functionsend

解决方案


您的初始化未正确完成,请尝试更改

char cfgred[7][10]={'lora_rf_config','915000000','10','0','1','8','14'};

进入

char cfgred[7][16]={"lora_rf_config","915000000","10","0","1","8","14"};

推荐阅读