简体   繁体   English

如何正确地将字符串数组存储到数组中(在 C 中)?

[英]How to correctly store array of strings into array (In C)?

*Edited* *已编辑*

Essentially, in a program I am writing I have initialised the following:本质上,在我正在编写的程序中,我已经初始化了以下内容:

char **cmd[16];
cmd[15] = '\0';

I then loop through an piped in text file which contains lines of text.然后,我循环遍历包含文本行的管道输入文本文件。 For example, when executing the program I will run./a.out < file.txt比如我在执行程序的时候会运行./a.out < file.txt

Within this loop, I break each line of the file into strings and store them in char *arguments[] .在这个循环中,我将文件的每一行分解为字符串并将它们存储在char *arguments[]中。

For example, the line of text "ls -n 5" creates arguments = {"ls", "-n", "5", NULL}例如,文本行 "ls -n 5" 创建arguments = {"ls", "-n", "5", NULL}

I then assign the pointer to arguments to an index of cmd based on the line number.然后,我根据行号将指向 arguments 的指针分配给 cmd 的索引。 Ie, cmd[line_number] = arguments;cmd[line_number] = arguments;

My problem arises when I exit the while loop.当我退出 while 循环时,我的问题就出现了。 Every index of cmd now points to the exact same thing, since what arguments points to has been changed.现在 cmd 的每个索引都指向完全相同的东西,因为 arguments 指向的内容已更改。 So even if I have a text file with:所以即使我有一个文本文件:

ls -s1
sort -n
ls -n 5

cmd will contain 3 arguments, which all have the last line {"ls", "-n", "5", NULL} cmd 将包含 3 个 arguments,它们都有最后一行{"ls", "-n", "5", NULL}

My question is whether there's a way I can assign the current value of arguments to cmd[line_number], without the value inside being overridden in the next iteration.我的问题是是否有一种方法可以将 arguments 的当前值分配给 cmd[line_number],而不会在下一次迭代中覆盖内部的值。

The following is the relevant code:以下是相关代码:

int main(int argc, char **argv) {

    char ** cmd[16];
    cmd[15] = '\0';

    char buffer[256];

    int number_of_lines = 0;

    // Initialise arguments
    char * arguments[256];

    // Read until EOF
    while (fgets(buffer, sizeof(buffer), stdin) != NULL) {

        // tokenise buffer into arguments
        // E.g. "Hello World" => arguments = {"Hello", "World", NULL}
        parsecmd(buffer, arguments);

        // Points to arguments
        cmd[number_of_lines] = arguments;

        number_of_lines++;
    }
    return 0;
}

Here is the parsecmd function as well:这里也是 parsecmd function :

void parsecmd(char *buffer, char **arguments) {

    // Get Pointer to \n in string
    char *newline = strchr(buffer, '\n');
    // Remove if found
    if (newline) {
        *newline = 0;
    }

    int i = 0;
    int token_count = 1;    
    // token equal to first to first string before whitespace in buffer
    char * token = strtok(buffer, " ");

    // While tokens remain
    while (token != NULL) {
        // Store token in arguments
        arguments[i] = token;

        // Get next token
        token = strtok(NULL, " ");

        // Increment counters
        i++;
        token_count++;
    } 

    // Assign all unnasigned variables to NULL 
    for (i = token_count - 1; i < 256; i++) {
        arguments[i] = NULL;
    }
}

Any help or suggestions would be much appreciated:)任何帮助或建议将不胜感激:)

suggest replacing:建议更换:

printf("%s\n", cmd[0][0]);

with:和:

printf("%s\n", args[0]);

because the output format specifier: %s is expecting a pointer to the string, not the first char of the string因为 output 格式说明符: %s期望指向字符串的指针,而不是字符串的第一个字符

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM