Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何传递char';将s转换为C中的函数_C_Arrays - Fatal编程技术网

如何传递char';将s转换为C中的函数

如何传递char';将s转换为C中的函数,c,arrays,C,Arrays,我正在制作我自己的命令提示符(学校项目),并试图跟踪用户使用的最后10个命令。所以我有一个数组: char* history[10]; 根据我的理解,这意味着我有一个指向字符串的指针数组。我的问题是,我有另一个变量,输入,即用户输入。但是每当用户输入新的内容时,输入的值就会改变,这意味着数组中的所有字符串都会改变为用户的新输入 我想知道我怎么才能摆脱这一切 我尝试将阵列更改为以下内容: char *history[10][MAX] //Where MAX = 256 我可以改为使用st

我正在制作我自己的命令提示符(学校项目),并试图跟踪用户使用的最后10个命令。所以我有一个数组:

char* history[10];
根据我的理解,这意味着我有一个指向字符串的指针数组。我的问题是,我有另一个变量,输入,即用户输入。但是每当用户输入新的内容时,输入的值就会改变,这意味着数组中的所有字符串都会改变为用户的新输入

我想知道我怎么才能摆脱这一切

我尝试将阵列更改为以下内容:

char *history[10][MAX]    //Where MAX = 256
我可以改为使用strcpy,但我不知道如何将数组数组输入到方法中,然后使用strcpy将字符串复制到数组中

以下是我目前的方法:

char* updateHistory(char *history[], char command[], int histIndex) {
    history[histIndex] = command;      
    return *history;
}
有其他解决方案的帮助吗,或者如何让我的解决方案起作用

意味着数组中的所有字符串都将更改为用户的新输入

发生这种情况可能是因为在
updateHistory
函数中有一个
command
引用的变量。因此,每当您在
updateHistory
函数的第一行赋值时,指针数组中的所有指针都指向相同的内存位置-
command

要解决此问题,您需要像这样分配指针数组(例如,您可以在函数外部执行此操作):


另外,不要忘记最后释放数组中的每个变量。

指针数组需要指向堆分配的内存,听起来好像指向某个发生变化的缓冲区

所以像这样的事情应该行得通

#define MAX_HISTORY 10

char* history[MAX_HISTORY];

if (fgets(input, sizeof(input), stdin) != NULL)
{
  input[strlen(input)-1] = '\0'; // remove \n
  history[n++] = strdup(input); // allocates and copies the input string
  if ( n == MAX_HISTORY )
  {
    // throw away the oldest and move pointers forward one step
  }
}
strdup
在概念上与

malloc( ) + strcpy() 
因此,当您向前移动指针时,当您想要清除历史记录时,您需要释放()指针指向的内容

或者,如果您不想使用堆,您可以使用一个大的缓冲区来放置历史记录


char history[MAX_history][MAX_CMD_LEN]
但是你需要转移更多的数据,这不是很优雅/有效,或者有一些复杂的索引系统来跟踪内容

当你想将指针数组传递给一个函数时,你可以在调用函数时使用“&”符号来传递地址。
例如:
这就是您声明的数组
char*history[10]
这是您使用的功能:

char* updateHistory(char *history[], char command[], int histIndex) {
    history[histIndex] = command;      
    return *history;
}
因此,在调用
main()
主体中的函数时,按如下方式调用它

main()
{
updateHistory(&history, command, histIndex);
}

我希望这对你有帮助。。好。

当您可以使用
malloc
calloc
在堆上自由分配空间时,如果您将历史记录限制在合理的大小,一个简单的二维静态声明字符数组也可以很好地工作。例如:

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

/* constants for max pointers, max chars */
enum {MAXP = 10, MAXC = 256};

int main (void) {

    char history[MAXP][MAXC] = {{0}};
    char buf[MAXC] = {0};
    size_t i, n = 0;

    while (printf ("prompt > ") && fgets (buf, MAXC, stdin)) {
        size_t buflen = strlen (buf);
        buf[--buflen] = 0;  /* strip newline */

        /* check buflen to prevent saving empty strings */
        if (!buflen) continue;

        strncpy (history[n++], buf, buflen);

        if (n == MAXP) /* handle full history */
            break;
    }

    for (i = 0; i < n; i++)
        printf (" history[%zu] : %s\n", i, history[i]);

    return 0;
}

从静态声明的数组中获得的好处是自动管理数组存储的内存,从堆栈中分配的内存在效率上略有提高。任何一个都可以,这只是您管理多少信息的问题。

最大值是否表示单个命令中的最大字符数?返回
return*history有什么意义?谢谢您的回答。这似乎是最容易遵循的方法,并且快速有效地解决了我的问题。@Anon很高兴它能帮上忙
main()
{
updateHistory(&history, command, histIndex);
}
#include <stdio.h>
#include <string.h>

/* constants for max pointers, max chars */
enum {MAXP = 10, MAXC = 256};

int main (void) {

    char history[MAXP][MAXC] = {{0}};
    char buf[MAXC] = {0};
    size_t i, n = 0;

    while (printf ("prompt > ") && fgets (buf, MAXC, stdin)) {
        size_t buflen = strlen (buf);
        buf[--buflen] = 0;  /* strip newline */

        /* check buflen to prevent saving empty strings */
        if (!buflen) continue;

        strncpy (history[n++], buf, buflen);

        if (n == MAXP) /* handle full history */
            break;
    }

    for (i = 0; i < n; i++)
        printf (" history[%zu] : %s\n", i, history[i]);

    return 0;
}
$ ./bin/fgets_static2d_hist
prompt > ls -al
prompt > mv a b/foo.txt
prompt > rsync ~/tmp/xfer hostb:~/tmp
prompt > du -hcs
prompt > cat /proc/cpuinfo
prompt > grep buflen *
prompt > ls -rt debug
prompt > gcc -Wall -Wextra -Ofast -o bin/fgets_static2d_hist fgets_static2d_hist.c
prompt > objdump obj/fgets_static2d.obj
prompt > source-highlight -i fgets_static2d.c -o fgets_static2d.html
 history[0] : ls -al
 history[1] : mv a b/foo.txt
 history[2] : rsync ~/tmp/xfer hostb:~/tmp
 history[3] : du -hcs
 history[4] : cat /proc/cpuinfo
 history[5] : grep buflen *
 history[6] : ls -rt debug
 history[7] : gcc -Wall -Wextra -Ofast -o bin/fgets_static2d_hist fgets_static2d_hist.c
 history[8] : objdump obj/fgets_static2d.obj
 history[9] : source-highlight -i fgets_static2d.c -o fgets_static2d.html