2016-02-05 65 views
4

我正在制作自己的命令提示符(学校项目),并且试图跟踪用户使用的最后10条命令。所以我有一个数组:如何将char数组传递给函数C

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

上的另一个解决方案任何帮助或如何让我的解决方案的工作?

+0

是MAX表示单个命令中的最大字符数? – Pooya

+0

返回'return * history;'的意义何在? –

回答

2

这意味着我的数组中的所有字符串都变为用户的新输入。

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

要解决这个问题,你需要分配的指针数组像这样(例如,你可以做到这一点你的函数外):

char *history[10]; 
for (i=0; i < 10; i++) 
{ 
    history[i] = malloc(MAXLEN); 
} 

然后复制字符串(这个可以去自己的函数中):

strcpy(history[i], command); 

也别忘了free数组中的每个变量最后。

+1

谢谢你的回答。这似乎是最容易遵循的,并且快速高效地解决了我的问题。 – Logan

+0

@Anon如果有帮助,很高兴 –

3

你的指针数组需要指向堆分配的内存,这听起来就好像你指向一定的缓冲改变

所以这样的事情应该工作

#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]大的缓冲区,但那么你就需要转移更多的数据和事实并非如此优雅/有效还是有一定的精心编制索引系统以跟踪内容

0

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

char* updateHistory(char *history[], char command[], int histIndex) { 
    history[histIndex] = command;  
    return *history; 
} 

所以,当调用的main()身体的功能,这样称呼它

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

我希望这会帮助你... ..好吧。

1

虽然你可以自由地在堆上分配空间malloccalloc,但如果将历史记录限制为合理的大小,则简单的2D静态声明字符数组可以同样适用。例如:

#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 

您从静态声明数组中得到的好处是你的阵列存储的自动内存管理和效率是一个轻微受益于内存从堆栈分配。要么会这样做,这只是你管理多少信息的问题。