首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用argv和argc向函数发送参数

使用argv和argc向函数发送参数
EN

Stack Overflow用户
提问于 2019-08-30 17:43:38
回答 2查看 80关注 0票数 0

有人能帮助我理解如何将参数发送到"lora_rf_config“函数吗?非常感谢!

我试着:

代码语言:javascript
复制
char cfgred[7][10]={'lora_rf_config','915000000','10','0','1','8','14'};
lora_rf_config(7,&cfgred);

我试图使用的功能是:

代码语言:javascript
复制
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;
}

我得到的错误是:

代码语言:javascript
复制
..\..\..\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'};

我对这种争论没有经验。谢谢您抽时间见我。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-08-31 06:05:43

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

C中的字符串是指向char的指针,它们指向的char缓冲区具有终止NUL字符(如果NUL char缺失,那么它不是字符串,只是一个字符数组)。换句话说,C中不存在字符串类型,但是字符串的严格性取决于char数组或缓冲区中的实际数据。使用""字符串文字可以创建一个字符串,IOW除了您所写的内容之外,它还添加了终止NUL。

代码语言:javascript
复制
// 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);

顺便提一下,总是打开编译器警告.他们帮了你很大的忙,修好了。对于gcc和clagn,使用-Wall -Wextra,对于Visual使用/W3或更好地使用/W4。然后修正你收到的任何警告,因为它们可能做不到你期望的事情。

票数 0
EN

Stack Overflow用户

发布于 2019-08-30 17:53:43

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

代码语言:javascript
复制
char cfgred[7][10]={'lora_rf_config','915000000','10','0','1','8','14'};

转到

代码语言:javascript
复制
char cfgred[7][16]={"lora_rf_config","915000000","10","0","1","8","14"};
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57731144

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档