⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 4_28.cpp

📁 C++程序设计技能百练随书配套光盘的源码
💻 CPP
字号:
# include<iostream.h>
# include<string.h>

char* fun(char(*p)[20],int n) //p是一个指针变量,它指向包含20个元素的一维数组
{
	int i;
	char* max;
	max=p[0];
	for (i=1;i<n;i++)
		if(strlen(p[i])>strlen(max))
			max=p[i];
		return max;
}

void main()
{
	int wsize;
	cout<<"Please enter the number of words :";
    cin>>wsize;		//临时分配元素的个数
    char(* a)[20];
	a=new char[wsize][20];//临时分配这些元素所需的内存空间(堆内存中)

	if(a!=NULL)		//判断,堆空间不够分配时,系统会返回一个空指针值NULL
	{
	   for(int i=0;i<wsize;i++)
	   {
	
		 	cin>>a[i];
	   }
	   cout<<"the words you inputted are:"<<endl;
	   for( i=0;i<wsize;i++)
	   {
			cout<<a[i];
		    cout<<"  ";
	   }
	   cout<<endl;
	   char*(*s)(char(*)[20],int);
	   s=fun;
	   char* max;
	   max=(*s)(a,wsize);
	   cout<<"The longest word is:"<<max<<endl;
	   delete[] a;	//释放堆内存
	}
    else
		cout<<"Can't allocate more memory."<<endl;

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -