小编Jas*_*nLi的帖子

字符串操作(剪切字符串的头部)

为什么我不能得到"xxx"?返回值是一些非常奇怪的符号...我希望返回的值是xxx,但我不知道这个程序有什么问题.该功能运行良好,可以为我打印"xxx",但一旦它返回值到main函数,字符串结果就不能很好地显示"xxx".有人可以告诉我原因吗?

char* cut_command_head(char *my_command, char *character) {
    char command_arg[256];
    //command_arg = (char *)calloc(256, sizeof(char));
    char *special_position;
    int len;
    special_position = strstr(my_command, character);
    //printf("special_position: %s\n", special_position);
    for (int i=1; special_position[i] != '\0'; i++) {
        //printf("spcial_position[%d]: %c\n", i, special_position[i]);
        command_arg[i-1] = special_position[i];
       //printf("command_arg[%d]: %c\n", i-1, command_arg[i-1]);
    }
    len = (int)strlen(command_arg);
    //printf("command_arg len: %d\n", len);
    command_arg[len] = '\0';
    my_command = command_arg;
    printf("my_command: %s\n", my_command);
    return my_command;
}

int main(int argc, const char * argv[]) {
    char *test = "cd xxx";
    char *outcome;
    outcome …
Run Code Online (Sandbox Code Playgroud)

c

2
推荐指数
1
解决办法
108
查看次数

标签 统计

c ×1