将C char数组拆分为单词

时间:2020-12-28 15:44:39

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.

我试图将给定的char数组拆分为单独的字符串。我这样做是通过将每个单词的地址放入一个数组,然后从地址中获取字符串进行打印。

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

所以我更新了我的代码,但现在程序在打印numArgs之后但在“test2”之前冻结。我不明白为什么。

----------------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();
}

3 个解决方案

#1


2  

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);

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).

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

#2


0  

Declare the pointer array 'char *args[100]' as global variable. In your program your are allocating memory to the local pointer and its life is within the function. so at the end of the function your pointer variable scope ends. Here there is memory leak too.

将指针数组'char * args [100]'声明为全局变量。在你的程序中,你正在为本地指针分配内存,它的生命在函数内。所以在函数结束时你的指针变量范围结束。这里也有内存泄漏。

#3


0  

The freeze is due to

冻结是由于

int i = 0;
while(token = strtok(comPtr, " ")){
    args[i] = token;
}

where you repeatedly - in an infinite loop - find the first token in comPtr, token becomes &comPtr[0] in each iteration (unless the string starts with spaces), and that is assigned to args[i].

你重复的地方 - 在无限循环中 - 在comPtr中找到第一个标记,在每次迭代中标记变为&comPtr [0](除非字符串以空格开头),并且分配给args [i]。

After the first call, all calls to strtok that shall find further tokens in the same string - if any - should have a NULL first argument.

在第一次调用之后,所有对strtok的调用都会在同一个字符串中找到更多的标记 - 如果有的话 - 应该有一个NULL的第一个参数。

Also, you should probably increment i in the loop, since presumably you don't want to overwrite args[0] with each new token.

此外,你应该在循环中增加i,因为大概你不想用每个新标记覆盖args [0]。

#1


2  

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);

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).

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

#2


0  

Declare the pointer array 'char *args[100]' as global variable. In your program your are allocating memory to the local pointer and its life is within the function. so at the end of the function your pointer variable scope ends. Here there is memory leak too.

将指针数组'char * args [100]'声明为全局变量。在你的程序中,你正在为本地指针分配内存,它的生命在函数内。所以在函数结束时你的指针变量范围结束。这里也有内存泄漏。

#3


0  

The freeze is due to

冻结是由于

int i = 0;
while(token = strtok(comPtr, " ")){
    args[i] = token;
}

where you repeatedly - in an infinite loop - find the first token in comPtr, token becomes &comPtr[0] in each iteration (unless the string starts with spaces), and that is assigned to args[i].

你重复的地方 - 在无限循环中 - 在comPtr中找到第一个标记,在每次迭代中标记变为&comPtr [0](除非字符串以空格开头),并且分配给args [i]。

After the first call, all calls to strtok that shall find further tokens in the same string - if any - should have a NULL first argument.

在第一次调用之后,所有对strtok的调用都会在同一个字符串中找到更多的标记 - 如果有的话 - 应该有一个NULL的第一个参数。

Also, you should probably increment i in the loop, since presumably you don't want to overwrite args[0] with each new token.

此外,你应该在循环中增加i,因为大概你不想用每个新标记覆盖args [0]。