c++ - Returning a pointer is not working fine -
when try display content of array via pointer returned function, program displays zeros. not sure missing. have tried check several times wrong, seem not have clue. using dev-c++. code bellow. appreciated.
#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}; (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}; (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]; (i=0; i<len_sent; i++) { c=sentence[i]; //check if c 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 output of pointer: "<<endl; (i=0; i<5; i++) cout<<ptr[i]<<" "; return ptr; } int main() { int j, length; char sentence[256]; int* ptr_array; cout<<"please, enter sentence; "; cin.getline(sentence,256); length=strlen(sentence); cout<<"the sentence: "; cout<<sentence<<endl; ptr_array=scan(sentence, length); //address of first element returned ptr_array cout<<endl; /// cout<<"upper case: "<<" lower case: "<<" digits: "<<" vowels: "<<" consonants: "<<endl; (j=0; j<5; j++) cout<<ptr_array[j]<<" "; //where problem is... return 0; }
ptr=array_all_counts;
ptr
local int*
, points local static array array_all_counts
, local array destroyed when exit function scan
. therefore, nothing inside main since memory pointed ptr
released.
you can try following inside scan
function make work:
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 block (int = 0; < 5 ; ++i) { ptr[i] = array_all_counts[i]; }
it work fine , print things follows if input "abcdefghijk"
:
testing output of pointer: 0 11 0 3 8 0 11 0 3 8
Comments
Post a Comment