使用 argv 和 argc 将参数发送到函数

Send arguments to a function with argv and 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'};

我没有这种争论的经验。 谢谢你的时间。

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

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"};

lora_rf_config 需要与 main 函数相同的参数:指向字符串的指针数组及其长度。

C 中的字符串是指向 char 的指针,它们指向的 char 缓冲区具有终止 NUL 字符(如果缺少 NUL char,则它不是字符串,只是一个字符数组)。也就是说,C中没有string类型,stringiness是由char数组或buffer中的实际数据决定的。使用 "" 字符串文字创建一个字符串,IOW 除了您编写的内容外,它还添加了终止 NUL 字符。

// cfgred is array([) of 7 pointers(*) to char.
// Note: string literals are read-only, so you must not modify these
// strings. If you want a modifiable string, this would be a bit more complex,
// but I think this is out of the scope of your question.
char *cfgred[7] = { "lora_rf_config" , "915000000", "10","0", "1", "8", "14"};

// you can get the number of elements in array by dividing its sizeof size (bytes)
// with the size of it's elements in bytes. Just make sure cfgred here is array...
// in the function it is pointer already (arrays get converted to pointers, so
// you can't do this inside the function, you have to do it where you still have
// the original array
int cfgred_len = sizeof cfgred / sizeof(cfgred[0]);

// when you pass array to function, it is automatically converted to pointer, 
// so you must not use & when passing an array like this, otherwise types don't
// match
lora_rf_config(cfgred_len, cfgred);

附带说明,始终打开编译器警告...它们对您有很大帮助,请修复它们。对于 gccclagn,使用 -Wall -Wextra,对于 Visual Studio 使用 /W3 或最好使用 /W4.然后修复您收到的所有警告,因为它们可能与您期望的不符。