【问题标题】:Pass parameters to the function in C将参数传递给C中的函数
【发布时间】:2018-09-19 06:42:52
【问题描述】:

我正在C 中编写一个函数来打开串口并读取芯片中的闪存数据。我想把串口参数传给这个函数,打开COM 4。 我试过用这些方法来调用函数:

  1. ret = download_main(1,'-C'+ 4);

  2. ret = download_main(1,'-C4');

  3. char array_1[] = { "-C4"};
    ret = download_main(1, array_1);

  4. ret = download_main(1, "-C4"); 并将函数定义更改为 int download_main(int argc, char *argv)
  5. char *args[] = { "-C4", NULL }; ret = download_main(1, args);

但都未能将参数传递给此函数并打开COM4。

函数调用

int main(int argc, char *argv[])
{
int ret;    

char* COMx = "com4";
FILE* fp;

printf("open com!\n");

/** Open Com */
if ((fp = fopen(COMx, "wb+")) == NULL) {
    printf("Open %s Failed!\n", COMx);
}

/** Send "Hello,world!" */
fwrite((char*)"Hello,world!", sizeof("Hello,world!"), 1, fp);


char *args[] = { "-C4", NULL };
ret = download_main(1, args);
return ret;
}

调用的函数:

int download_main(int argc, char *argv[])
{   
BOOL fSuccess;
TCHAR commPort[16];
TCHAR *numStr;
int len, i,ch;
int poweroff_param;
char *optstring = "C:c:r:F:f:P:w:b:B:R:A:l:h";
bininfo_node *bin_info = NULL;

opterr = 1;
if (argc < 3)
{
    usage();
    return 0;
}


optind = 0;
while((ch=getopt(argc,argv,optstring))!=-1)
{
    switch(ch)
    {
        case 'c':
            g_calibrate_switch = atoi(optarg);
            if(1 == g_calibrate_switch)
                printf("burn flash then entry calib mode\n");
            break;
        case 'C':
            numStr = optarg;
            len = _tcslen(numStr);
            for (i = 0; i < len; i++) 
            {
                if (!_istdigit(numStr[i])) 
                {
                    TRACE("Error: Invalid COM number: %s", numStr);
                    g_dldtool_exit_code = EXIT_CODE_COM_NUM_ERROR;
                    goto _exit;
                }
            }
            _stprintf_s(commPort, sizeof(commPort) / sizeof(TCHAR), "\\\\.\\COM%s", optarg);
            break;

这个程序没有编译错误。当我调试这个程序时,它一直以代码 0 退出并提醒我向函数添加参数,而无需通过 download_main 函数中的断点,如下所示:

【问题讨论】:

  • 将 int download_main(int argc, char *argv[]) 更改为 int download_main(int argc, char *argv) 并传递类似 ret = download_main(1,"-C4") 的参数。如果您打算传递多个字符串,那么您的原型是正确的。
  • 您知道'-C4' 不是字符串,对吧...?
  • @CreateFile 你看到 if (argc
  • 你必须学会​​区分不同类型的失败。您的一些错误是编译器告诉您的语法错误。其他是运行时错误,可以使用调试器找到。

标签: c getopt


【解决方案1】:

由于您似乎使用与 main 相同的机制,因此您必须正确准备字符串:

char *args[] = {"download_main", "-C4", NULL};
ret = download_main(2, args);

更新: 正如@rici 指出的那样,在扫描之前设置optind=0 并不意味着第一个参数由getopt 传递。因此,您必须提供额外的第一个参数。通常,当使用参数列表调用 main 时,它包含程序的名称,但对于您自己的函数调用,第一个字符串中的内容并不重要。

【讨论】:

  • 还是不行……程序还是让我传参数。
  • @CreateFile 你能发布你得到的 Gerhardh 答案的编译器错误吗?
  • @CreateFile 你看到if (argc &lt; 3) { usage(); return 0; } 你必须传递至少 3 个参数。
  • @CreateFile 至少它应该编译没有错误。如果您的函数告诉您添加参数,您需要重新考虑检查参数(argc&lt;3),正如@kiranBiradar 提到的那样。使用调试器,您应该会在几秒钟内看到原因。请始终先使用调试器。
  • 由于download_main 模拟一个顶级主函数,它期望argv 数组中的第一个元素是它的名字。它使用getopt 来解析它的参数列表,并且以argv[1] 开头。所以你需要char *args[] = {"download", "-C4", NULL}; ret = download_main(2, args); 或类似的东西;第一个元素是任意的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-29
  • 2012-12-02
  • 2017-10-04
  • 2013-01-27
  • 2012-09-26
相关资源
最近更新 更多