将C char数组拆分为单词 [英] Splitting C char array into words

查看:107
本文介绍了将C char数组拆分为单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将给定的char数组拆分为单独的字符串.为此,我将每个单词的地址放入数组中,然后从地址中获取字符串以进行打印.

I am trying to split a given char array into separate strings. I am doing this by putting the address of each word into an array, and then getting the string from the address to print.

所以我已经更新了代码,但是现在程序在打印numArgs之后但在"test2"之前冻结.我不明白为什么.

So I have updated my code but now the program freezes after printing the numArgs but before "test2." I don't understand why.

----------------old code-----------------------   
char* parseArgs(char* comPtr){
    char *args[100] = {0};
    char *token;
    int i = 0;
    token = strtok(comPtr, " ");
    while(token != NULL){
        args[i] = malloc(100);
        args[i] = &token;
        token = strtok(NULL, " ");
    }
    return *args;
}

char* args = parseArgs(comPtr);
int i = 0;
while(i < numArgs){
    printf("arg%d: %s\n",i,&args[i]);
    i++;
}
-----------------------end old code--------------------

------------------new code------------------------
int countArgs(char* comPtr){
    char *token;
    int i = 0;
    token = strtok(comPtr, " ");
    while(token != NULL){
        i++;
        token = strtok(NULL, " ");
    }
    return i;
}

char** parseArgs(char* comPtr){
    printf("test1");
    char** args = calloc(100, sizeof(char*));
    char* token;
    int i = 0;
    while(token = strtok(comPtr, " ")){
        args[i] = token;
    }
    printf("test2");
    return args;
}

printf("ComPtr: %s\n",comPtr);
char* path = "/bin/";
//int pid = fork(); //pid always 0 so using pid = 1 to test
//printf("PID:%d",pid);
int pid = 1;
printf("PID:%d",pid);
if(pid != 0){
    int numArgs = countArgs(comPtr);
    printf("test1");
    printf("NumArgs: %d\n",numArgs);
    printf("test2");
    char** args = parseArgs(comPtr);
    int i = 0;
    printf("test3");
    while(i < numArgs){
        printf("arg%d: %s\n",i,args[i]);
        printf("test4");
        i++;
    }
}
else{
    //waitpid();
}

推荐答案

您已经失去对内存位置,指针所指向的位置等的了解.如果要返回令牌的指针列表,则需要一些东西像这样:

You've lost track of where your memory is, your pointers are pointing etc.. If you want to return the list of pointers to tokens, you need something like this:

char** parseArgs(char* comPtr){
    char** p_args = calloc(100, sizeof(char*);
    int i = 0;
    char* token;
    while (token = strtok(comPtr, " "))
        p_args[i] = token;
    return p_args;
}

char** p_args = parseArgs(comPtr);
int i = 0;
while(i < numArgs)
{
    printf("arg%d: %s\n",i,p_args[i]);
    i++;
}
free(p_args);

我还没有测试过,但是它应该为您指明正确的方向.请仔细考虑它与程序的不同之处,并在代码中使用调试器和/或printf()语句以打印出地址并查看其工作方式(或在必要时进行调试).

I haven't tested it, but it should point you in the right direction. Have a careful think about how it differs from your program, and use a debugger and/or printf() statements in the code to print out addresses and see how it works (or debug it if necessary).

这篇关于将C char数组拆分为单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆