0

两个小时以来,我试图修改我的程序以给它参数(argv)而不是一个字符。

所以,这是我当前的代码:

int i;
 char ret[81];
    *ret = 1;
    for (i = 0; i < argc; i++)
    { 
            ret[0] = '\0';
          strcat(ret,argv[i]);
    }

此代码将所有 args 连接成一个 char,printf 返回的结果与我的旧 char 参数相同,但在我的代码中不起作用:

char test[] = "9...7....2...9..53.6..124..84...1.9.5.....8...31..4.....37..68..9..5.74147.......";
        solve(test); //working
        solve(ret); //not working

我的应用程序是这样启动的:./a.out "9...7...." "2...9..53" ".6..124.." "84...1.9." “5.....8..” “.31..4...” “..37..68.” “.9..5.741” “47........”

Soooo,如果有人理解我的问题,我可能需要一些帮助:D

4

1 回答 1

0

示例代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void solve(char *data){
    static const char *result = "9...7....2...9..53.6..124..84...1.9.5.....8...31..4.....37..68..9..5.74147.......";
    if(strcmp(result, data) == 0)
        printf("working\n");
    else
        printf("not working\n");
}

int main(int argc,  char *argv[]){
    int i, total_length = 0;

    for(i = 1; i < argc; ++i){
        total_length += strlen(argv[i]);
    }

    char ret[total_length + 1];

    ret[0] = '\0';
    for(i = 1; i < argc; ++i){
        strcat(ret, argv[i]);
    }
    char test[] = "9...7...."
                  "2...9..53"
                  ".6..124.."
                  "84...1.9."
                  "5.....8.."
                  ".31..4..."
                  "..37..68."
                  ".9..5.741"
                  "47.......";
    solve(test);
    solve(ret);

    return 0;
}
于 2013-07-28T13:45:24.890 回答