2014-04-13 74 views
0

我試圖通過硬編碼打印輸出,但我得到一個錯誤,因爲我給出的參數是類型char**和格式在printf中指定了char *類型。格式指定類型'char *',但參數具有類型'char **'

此外,還有四行代碼,我不明白(請參閱下面的代碼中的代碼註釋),所以這將是真正有用的是有人解釋代碼塊。在你的代碼

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

void inputParsing(char *src, char *end, char *destU, char *destP) { 
    int x = 0; 
    for(; src != end; src++){ 
     if((*src != '+') && x==0) { 
      *destU = *src; 
      destU++; 
     } 
     else if((*src != '+') && x==1){ 
      *destP = *src; 
      destP++; 
     } 
     else { 
      x = 1; 
     } 
    } 
    *destU = ' ';     //What does this line do? 
    *destP = ' ';     //What does this line do? 
    *++destU = '0';    //What does this line do? 
    *++destP = '0';    //What does this line do? 
    printf("%s\n",&destU); 
    printf("%s\n",&destP); 
} 

void inputStoring() { 
    char inputArray[200]; 
    char usernameArray[200]; 
    char passwordArray[200]; 
    //int n = atoi(getenv("CONTENT_LENGTH")); 
    //fgets(inputArray, n+1, stdin); 
    strcpy(inputArray, "gaming+koko"); 
    int n = strlen(inputArray); 
    inputParsing(inputArray, inputArray + n, usernameArray, passwordArray); //inputArray+n is referencing the array cell that contains the last inputted character. 
} 

int main(void) { 
    inputStoring(); 
} 
+1

此代碼從何而來?它的目的是什麼? (不知道這一點,我們只能推測個別行的目的...) –

+1

我認爲你會對你標記的那兩行*之後更感興趣,因爲它們是那些在程序中調用*未定義的行爲*。標記行只是使用指針解引用操作符將來自調用者的提供緩衝區中的單個字符存儲起來。 (使用第二對增加存儲位置)。 – WhozCraig

+0

@OliCharlesworth - 我寫了這段代碼,通過POST方法從網站獲取輸入。所以基本上我將輸入存儲在inputArray中,然後調用方法inputParsing來分隔用戶名和密碼並將它們存儲在兩個不同的數組中。 – user1836292

回答

0

after correc婷char* VS char** -issue和'0' VS '\0' -issue

*++destU = '\0';    //What does this line do? 
*++destP = '\0';    //What does this line do? 
printf("%s\n",destU); 
printf("%s\n",destP); 

的代碼仍然沒有任何用處,因爲destUdestP將分別指向一個空字節,這意味着printf()將達到'\0'作爲第一字符,並且不會打印任何東西。 你可能應該解釋一下代碼假設要做什麼,所以我們可以說出錯的地方。

+0

謝謝,現在我明白了爲什麼它不打印我想要打印的東西。我現在可以解決其餘的問題。 – user1836292

0

destU和destP是字符*。但是你在printf中傳遞它們的符號是&,這意味着你傳遞了他們的地址,所以printf得到一個字符指針的地址。

ü需要通過只是destU和destP給printf函數作爲

printf("%s\n",destU); 
printf("%s\n",destP); 

這裏是指向小導我在谷歌發現一個簡單的搜索 http://pw1.netcom.com/~tjensen/ptr/pointers.htm

*destU = ' ';   /*Sets "space character" or ' ' at the current destU pointer position*/   
*destP = ' ';   /*Same but for destP*/     
*++destU = '0';  /*Moves pointer position by 1 place forward and sets a value 0 byte in its place - this is not a string terminating value (\0 is) so u may have problems when trying to print the string thank you EOF for correcting*/ 
*++destP = '0';  /*Same but for destP*/ 

也inputArray + n將是一個\ 0字節的值,所以你可以問這個值是否爲0(每個字符串都以\ 0結尾)

+1

''0''是**不是**空終止符。 ''\ 0''是。 – EOF

+0

現在根據你所發佈的內容改變代碼後,不應該第一次printf打印「遊戲」,第二次打印「科科」?但是當我運行我的程序時,它不會打印任何東西。 – user1836292

+0

是的,因爲='0'你需要把'\ 0'代替。 –

相關問題