2016-11-21 425 views
1

每當我嘗試運行我的代碼時,彈出窗口即將告知program.exe已停止工作。Code :: Blocks錯誤.exe已停止工作

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

int main() 
{ 
    char D[10]; 
    for(int i; D[i]!='\0';i++) 
     scanf("%c",D); 
    for(int i; D[i]!='\0';i++) 
     printf("%c",D); 

    return 0; 
} 

這裏是多了一個,這是造成相同的錯誤

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

int main() 
{ 
    int *ptr= (int*)1000; 
    printf("%d %d ",ptr,*ptr); 
    ptr=ptr+1; 
    printf("New Value of ptr : %u",ptr); 
    printf("%d %d ",ptr,*ptr); 
    return 0; 
} 
+2

你可以發佈代碼嗎?如果我們無法看到代碼,那麼我們有點難以確定問題所在。編輯問題以包含它。 – GrandMasterFlush

+0

https://drive.google.com/file/d/0B-QBwnCjr6U9ZzR4ODc2NzdlYW8/view?usp=sharing –

+0

@GrandMasterFlush下面是另一個代碼中同樣錯誤的屏幕截圖。 https://drive.google.com/file/d/0B-QBwnCjr6U9Z3BwLWpaU1JGZEk/view?usp=sharing –

回答

0

代碼:: Blocks的錯誤的.exe已停止工作與代碼:: Blocks的一個非常通用的錯誤代碼。它可能出於許多不同的原因,所以這兩種情況都有不同的錯誤。


在第一個,D值未在for環在使用之前設置。當您測試D[i] != '\0'時,行爲可能不可預測。

然後,您沒有初始化您的for循環中的i。你的意思是

for(i = 0; D[i] != '\0'; i++)

最後的printf不正確,您不能打印char數組。你的意思是

printf("%c", D[i]);


我不知道關於第二個,但我敢肯定,第一行int *ptr= (int*)1000;不正確。 嘗試

int a = 1000; 
int *ptr = &a; 
0

在你的第一個程序中,你還沒有初始化我。你應該把int i = 0放在for循環中。

此外,您的程序是一個無限循環。因爲你每次只在D [0]掃描。你確定你不想使用do ... while循環嗎? 你也不需要第二個循環。你可能想看看如何在c中掃描和打印一個字符串。

您可能想寫:

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

int main() 
{ 
    char D[10]; 
    scanf(" %[^\n]s", D); 
    printf("%s\n", D); 

    return 0; 
} 

在第二個程序中使用:

int a = 1000; 
int *p = &a; 

這確保了整都有它的內存地址保留。