gpt4 book ai didi

c++ - 返回指针无法正常工作

转载 作者:行者123 更新时间:2023-11-28 07:39:34 25 4
gpt4 key购买 nike

当我尝试通过函数返回的指针显示数组的内容时,程序只显示零。我不确定缺少什么。我试过几次检查出了什么问题,但似乎没有任何线索。我正在使用 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;
}

最佳答案

   ptr=array_all_counts;

ptr是一个局部的int*,它指向一个局部的静态数组array_all_counts,这个局部数组会在函数退出时销毁扫描。因此,由于 ptr 指向的内存已释放,您在 main 中什么也得不到。

您可以在 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

关于c++ - 返回指针无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16116395/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com