2013-04-20 103 views
0

當我嘗試通過函數返回的指針顯示數組的內容時,程序只顯示零。我不知道缺少什麼。我試圖多次檢查什麼是錯誤的,但似乎沒有線索。我正在使用Dev-C++。該守則是咆哮。您的幫助將非常感激。返回指針不能正常工作

#include <iostream> 
#include <math.h> 
#include <cstdlib> 
#include <cstring> 

using namespace std; 

bool vowel(char c) 
{ 
int i, val; 
char alphabet[52]={'a','A','b','B','c','C','d','D','e','E','f','F','g','G','h','H','i','I','j','J','k','K','l','L','m','M','n','N','o','O','p','P','q','Q','r','R','s','S','t','T','u','U','v','V','w','W','x','X','y','Y','z','Z'}; 
int const is_vowel[52]={1,1,0,0,0,0,0,0,1,1,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,1,1,0,0}; 

    for (i=0;i<52;i++) 
    if (c!=alphabet[i]) 
    { 
    val=is_vowel[i]; 
    return val; 
    }  

} 

bool consonant(char c) 
{ 
    int i, val; 
char alphabet[52]={'a','A','b','B','c','C','d','D','e','E','f','F','g','G','h','H','i','I','j','J','k','K','l','L','m','M','n','N','o','O','p','P','q','Q','r','R','s','S','t','T','u','U','v','V','w','W','x','X','y','Y','z','Z'}; 
    int const is_coson[52]={1,1,0,0,0,0,0,0,1,1,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,1,1,0,0}; 

    for (i=0;i<52;i++) 
    if (c==alphabet[i]) 
    { 
    val=is_coson[i]; 
    return val; 
    }  

} 

int* scan(char* sentence, int len_sent) 
{ 
    char c; 
int count_cons=0, count_vow=0, count_dig=0, count_lc=0, count_uc=0, i,j; 
int con_value, vow_value; 
int* ptr; 
int array_all_counts[5]; 

for (i=0; i<len_sent; i++) 
{ 
    c=sentence[i]; 

    //check if the c is a digit 
    if (c>=48 && c<=57) 
     count_dig++; 

    else if (isalpha(c)) 
    { 
     con_value=consonant(c); 
     vow_value=vowel(c); 
     if (con_value==0) 
      count_cons++; 

     else if (vow_value!=0) //vow_value==1 
      count_vow++; 

     if (96<c && c<123) 
      count_lc++; 

     if (64<c && c<91) 
      count_uc++;     
    } 

} 
cout<<"\n-------------------"<<endl; 

array_all_counts[0]=count_uc; 
array_all_counts[1]=count_lc; 
array_all_counts[2]=count_dig; 
array_all_counts[3]=count_vow; 
array_all_counts[4]=count_cons; 

    ptr=array_all_counts; 


    cout<<"\n\n\nTesting the output of pointer: "<<endl; 
    for (i=0; i<5; i++) 
     cout<<ptr[i]<<" "; 

return ptr; 

} 


int main() 
{ 

    int j, length; 

char sentence[256]; 
int* ptr_array; 

cout<<"Please, enter a sentence; "; 
cin.getline(sentence,256); 
length=strlen(sentence); 
cout<<"the sentence: "; 
cout<<sentence<<endl; 

ptr_array=scan(sentence, length); //Address of first element returned into ptr_array 

cout<<endl; 
/// cout<<"Upper case: "<<" Lower case: "<<" Digits: "<<" Vowels: "<<" Consonants: "<<endl; 
for (j=0; j<5; j++) 
    cout<<ptr_array[j]<<" "; //Where the problem is... 

return 0; 
} 

回答

1
ptr=array_all_counts; 

ptr是本地int*,它指向一個本地靜態數組array_all_counts,本地陣列將被破壞時,從功能scan退出。因此,由於ptr指向的內存被釋放,所以主內部沒有任何內容。

你可以試試你的scan功能裏面以下,使其工作:

int* ptr = new int [5]; //allocate memory on heap 

array_all_counts[0]=count_uc; 
array_all_counts[1]=count_lc; 
array_all_counts[2]=count_dig; 
array_all_counts[3]=count_vow; 
array_all_counts[4]=count_cons; 

//add this block 
for (int i = 0; i < 5 ; ++i) 
{ 
    ptr[i] = array_all_counts[i]; 
} 

它會正常工作和打印的事情如下,如果輸入的是"abcdefghijk"

Testing the output of pointer: 
0 11 0 3 8 
0 11 0 3 8 
+0

它工作正常,謝謝。但是,添加該塊後,ptr仍然是一個本地int *權利?那麼,爲什麼添加該塊使其現在可以工作? – T4000 2013-04-20 04:18:18

+0

1)當函數返回時,所有局部變量都被銷燬。 2)指針是存儲其他變量地址的變量。 ptr是一個局部變量並不重要--ptr中包含的地址將被複制並返回到return語句中。問題是你的數組是本地創建的,所以當你的函數結束時,數組將不復存在。因此,您返回了不再存在的地址,因此行爲未定義。 – 7stud 2013-04-20 04:20:33

+0

@ 7stud:清晰,非常清晰的解釋!謝謝! – T4000 2013-04-20 04:25:35