2017-04-22 42 views
2

我正在C編程Arduino板,因此無法打印,除非我做串行通信到外部終端窗口。如何從c中的char *訪問char []?

因爲這一點,我已經開發出一種printAll方法:

void printAll(char * str) { 
    int i; 
    for(i = 0; str[i] != 0x00; i++) { 
    PSerial_write('0', str[i]); 
    } 
} 

我也有一個變量:

char input[12]; 
input[0] = 'h'; 
input[1] = 'e'; 
input[2] = 'l'; 
input[3] = 'p'; 

我想要做的就是通過這個數組到printAll方法(但printAll方法需要一個char *)。

我試圖做:

printAll(&input[0]); 

但沒有被顯示出來!但是,當我一步一步打印輸入數組的每個字符時,我得到:

help<0><0><0><0><0><0><0><0> 

任何人都可以解釋爲什麼這不起作用嗎?謝謝!

***注:

printAll("Hello World!"); 

總體我的代碼看起來是這樣的:

char input[12]; 

int main(void) { 
    start(); 
} 

void start() { 
    while(1) { 
    printAll("Please enter in a command!\r"); 
    printAll("Please type 'help' for instructions!\r"); 
    char input[12]; 
    readInput(input); 
    printAll("Trying to print the char array stuff....\r"); 
    printAll(input); 
    if (input == "help") printHelp(); 
    else if (input == "set") { 
     if (setStatus) printSet(); 
     else printAll("Controller not authorized to print.\n"); 
    } 
    else if (input == "set on") setStatus = true; 
    else if (input == "set off") setStatus = false; 
    else if (input == "set hex=on") hexFormat = true; 
    else if (input == "set hex=off") hexFormat = false; 
    else if (input == "set tlow") tlow = getNumber(input); 
    else if (input == "set thigh") thigh = getNumber(input); 
    else if (input == "set period") period = getNumber(input); 
    x_yield(); 
    } 
} 

void readInput() { 
    char c = PSerial_read('0'); //reads character from user 
    while (c != '\r') { 
    //while the character isnt 'enter' 
    input[currIndex] = c; 
    c = PSerial_read('0'); 
    currIndex++; 
    } 
    int y; 
    for(y = 0; y < 12; y++) { 
    PSerial_write('0', input[y]); 
    //go through input and print each character 
    } 
    PSerial_write('0', '\r'); 
    //add new line to print log 
    currIndex = 0; //reset for next input (overwrites current stuff) 
} 

現在不管我輸入時,它使用像這樣當printAll方法工作完全正常只是要求更多的輸入,並且在輸入方法返回後決不會輸出數組。

+1

也做'input [4] = 0;'在打印 –

回答

2

您發送的代碼是混合的並且不會編譯。代碼表明你有兩個輸入變量,一個是全局變量,一個是本地變量。 readInput()讀取全局變量和printAll()本地變量(反之亦然,取決於哪個代碼已更改)。刪除全局輸入,不應該使用全局輸入,並將相同的輸入變量傳遞給readInput()和printAll()。

+0

是的,它會做到這一點。 idk我錯過了那個,感謝你的好眼睛! – skyleguy

+0

雖然我有你,但是......它正確地打印我輸入的內容,但是如果它匹配if語句塊中的關鍵字,它不會執行它們。我沒有使用正確的比較來檢查輸入和這些字符串文字之間的相等性嗎? JK我只是做了一個快速搜索,發現strcmp做的工作 – skyleguy

+0

if(strncmp(input,「help」,sizeof(input))== 0)printHelp(); – hdante

1

請注意,字符串文字「Hello World!」是一個char []。它真正打印的事實意味着你的代碼有其他問題。請發佈您嘗試運行的整個代碼。

此外,請注意,char []會在引用時自動轉換爲char *。這被稱爲數組衰減成指針。請參閱:

Why do arrays in C decay to pointers?

所以,既:

printAll(&input[0]); 

而且

printAll(input); 

做同樣的事情:它們將輸入到一個char *。

編輯:從您的較大的代碼我可以至少看到,你沒有添加一個'\ 0'字符後閱讀和打印時不發送'\ r'。字符串比較也是不正確的,應該用strcmp()完成。

+0

之前編輯它。這是我迄今爲止所有的。我確實有if/else if語句使用的方法,但代碼沒有得到那麼多,所以我看不到需要發佈它們 – skyleguy

+0

我已經添加了一些註釋,但我建議先從一個乾淨的代碼開始,然後只定義陣列並打印它。 – trollkill

1

我對Arduinos瞭解不多,所以請在這裏忍受。

在您的readInput函數中,您沒有包含input數組的任何參數,這意味着您正在使用在此函數的代碼開頭聲明的全局變量。但是,在start函數中,即使該函數沒有使用任何參數,也可以聲明另一個input數組並將其傳遞給readInput

這個新input實際上將「隱藏」了它在聲明的功能全局input變量。這意味着start使用這個地方input即使readInput使用全局input。換句話說,你實際上使用了兩個不同的變量,而不是一個。

要解決該問題,請嘗試去除start無論是char input[12],或者在你的代碼的開頭刪除全局char input[12]並將其添加爲一個參數爲readInputvoid readInput(char * input)

注:void readInput(void):即使你沒有包括在readInput任何參數,C編譯器不會,如果傳遞參數,除非你放在括號void抱怨。

+0

我不知道c編譯器!這將有益於未來的感謝! – skyleguy

+0

儘管我有你,但它...正確地打印了我輸入的內容,但是如果它匹配if語句塊中的關鍵字,它不會執行它們。我沒有使用正確的比較來檢查輸入和這些字符串文字之間的相等性嗎? JK我只是做了一個快速搜索,發現strcmp做這個工作 – skyleguy

+2

當你比較一個字符串時,你必須使用'strcmp()'函數。使用'=='運算符將把數組視爲一個指針。 –