[英]C - scanf changes the values of an array without direct access
這是我第一次來stackoverflow。 我希望我的問題適合。 我們本學期開始在大學編程 C。 不幸的是,只有少數在線講座。 但我們仍然必須解決任務。
這次我們應該編寫一種 Hang-Man。 換句話說,猜一個隱藏的單詞。 我有以下問題。 我得到一個字符,但輸入它后,謎語數組的內容發生了變化。 如果我省略輸入,它會起作用。 我不明白為什么會發生這種情況,因為 scanf 實際上並沒有訪問謎語。 我自己不知道在這里做什么。 我希望有人能告訴我代碼有什么問題。
//sry some variables and texts are in german
char* createRiddle(char const* const str){
int laenge = strlen(str);
char temp[laenge+1];
char *t = temp;
strcpy(temp, str);
int te = strcmp(temp, str);
if (te != 0){
printf("ERROR: Bei der Speicherreservierung ist ein Fehler aufgetreten");
exit(0);
}
int i;
for (i=0; i < (int)strlen(temp);i++){
if (str[i] > 65 && str[i] < 90){ //ASCII Großbuchstaben-Bereich prüfen
char verdeckt = '*';
temp[i] = verdeckt;
} else {
temp[i] = str[i];
}
}
return t;
}
//----------------------------
int uncoverLetter(char *riddle, const char *solution, char letter){
printf("RD3: %s\n",riddle);
letter = toupper(letter);
int i;
int treffer = 0;
for (i=0; i < (int)strlen(solution); i++) {
if (letter == solution[i]) { // Buchstabe im Wort?
if (letter != riddle[i]) { //Buchstabe schon aufgedeckt?
riddle[i] = solution[i];
treffer = treffer + 1;
}
}
}
return treffer;
}
//----------
int gamingLoop(const char* solution){
int punkte; //points
printf("Lets GO!\n\n");
char *riddle = createRiddle(solution);
printf("Gesuchtes Wort: %s\n\n",riddle); //Word: *-******* ( = C-Compiler )
int highscore = 0;
while ((strcmp(riddle, solution)) != 0) {
printf("RD1: %s\n",riddle); //Test: What does Riddle look like?
printf("Bitte geben Sie einen Buchstaben ein: "); // pls enter letter
char eingabe;
scanf(" %c", &eingabe); //-----!!Here is the point where things go wrong!!------
printf("RD2: %s\n",riddle); //Test2
int treffer = uncoverLetter(riddle, solution, eingabe);
//----------- probably unimportant for the problem ----------------
//Zufallszahl
int zufz = (rand() % 11) + 1;
int ii = 1;
for (ii=1; ii < 11 ; ii++){
if ( zufz == ii) {
punkte = zufz*100;
}
}
//------------
if (treffer != 0) {
printf("Du hast %d richtige Treffer.\n", treffer);
highscore = highscore + (treffer*punkte);
printf("Punkte: %i\n\n", highscore);
} else {
printf("Du hast leider keinen Treffer.\n");
highscore = highscore - punkte;
printf("Punkte: %d\n\n", highscore);
}
printf("%s\n\n",riddle);
}
return highscore;
}
OUTPUT:抱歉沒有圖片,因為我沒有 10 個代表:(
// 函數中的 R3 unlockLetter
我強烈懷疑我犯了一個非常愚蠢的錯誤,但不幸的是我自己看不到/還看不到。 我期待着建議和幫助。
謝謝你。
您的問題出在createRiddle
中,您在其中創建了***
模式:
char* createRiddle(char const* const str){
int laenge = strlen(str);
char temp[laenge+1];
char *t = temp;
// ... create pattern ...
return t;
}
您返回一個本地數組。 ( t
只是數組temp
的別名。)當 function 退出並因此無效時,該數組將超出 scope 。
有幾種可能的解決方案。
讓調用者提供空間
傳入一個調用者可以填充的數組:
void createRiddle(char *temp, char const* const str)
{
// ... create pattern in temp ...
}
然后像這樣調用它:
char riddle[MAX];
createPattern(riddle, solution);
您不需要在這里返回數組,因為它與您提供的數組相同,只是填充了。 (如果它使調用更容易,你可以返回它。你也可以返回一個錯誤代碼。使用你的良好判斷力。)
當然,function 和調用者需要就必須提供多少空間達成一致。 (這可能是另一個 function 參數或全局常量。)
動態分配 memory
動態 memory 在堆上分配,保證不被其他人使用:
char *createRiddle(char const* const str)
{
int laenge = strlen(str);
char *temp = malloc(laenge + 1);
// ... create pattern in temp ...
return temp;
}
然后像這樣使用它:
char *riddle = createRiddle(char const* const str);
// ... play the game ...
free(riddle); // be nice and clean up
Static arrays
制作數組static
。
static char temp[laenge+1];
在這里, static
關鍵字意味着只有一個數組在調用之間保留其值。 就好像您已將數組聲明為 function 之外的全局數組,但另外它的名稱只有您的 function 知道。
這是一個快速簡單的解決方案,但是當您的 function 是遞歸的或當您的代碼的其他部分使用相同的 function 時,它會失敗。 (不過,您的游戲中可能並非如此。)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.