返回指针不能正常工作
Returning a pointer is not working fine
当我试图通过函数返回的指针显示数组的内容时,程序只显示零。我不确定缺少了什么。我试着检查了几次是什么问题,但似乎没有线索。我正在使用devc++。代码如下。非常感谢您的帮助。
#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<<"nnnTesting 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
,当退出函数scan
时,局部数组将被破坏。因此,由于释放了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
相关文章:
- QSqlquery prepare()和bindvalue()不工作
- 导入库可以跨dll版本工作吗
- 以螺旋方式打印矩阵的程序.(工作不好)
- 对象指针在c++中是如何工作的
- 为什么在Windows上的VS 2019和Clang 9中"size_t"在没有标题的情况下工作
- VSOMEIP-2个设备之间的通信(TCP/UDP)不工作
- 为字符串中每 N 个字符插入空格的函数没有按照我认为的方式工作?
- C++为线程工作动态地分割例程
- 为什么我的 std::ref 无法按预期工作?
- 布尔比较运算符是如何在C++中工作的
- SampleConsensusPrerejective(ext.RANSAC)是如何真正工作的
- 不确定要在我的main中放入什么才能使我的代码正常工作
- 为什么std::condition_variable notify_all的工作速度比notify_one快(对于随机请
- <<操作员在下面的行中工作
- 有人能解释一下为什么下界是这样工作的吗C++的
- ExtractIconEx:可以工作,但偶尔会崩溃
- C++中的memset函数工作不正常
- 为什么STD ::计数将常数传递给Lambda,而不是在弦上工作时而不是字符
- sdl软件渲染器不工作,工作在硬件加速的一个
- C++程序已停止工作-求解常微分方程