2016-02-05 110 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 

您從靜態聲明數組中得到的好處是你的陣列存儲的自動內存管理和效率是一個輕微受益於內存從堆棧分配。要麼會這樣做,這只是你管理多少信息的問題。