上面的有点小错误
更正以后是
#include <iostream>
#include <time.h>
using namespace std;
/*
this is a function to dispaly array of string
@count is the number of strings
@value is the first address of the first string
*/
bool display(int count,char **value)
{
for(int i=0;i<count;i++)
if(*value[i] == NULL)
return false;
else cout<<value[i]<<endl;
}
void main()
{
char *p[5];
for(int i=0;i<5;i++)
{
p[i]=new char[10];
//sprintf(p,"hello%d",i);
sprintf(p[i],"hello%d",i);
cout<<p[i]<<endl;
#include <iostream>
#include <time.h>
using namespace std;
/*
this is a function to dispaly array of string
@count is the number of strings
@value is the first address of the first string
*/
bool display(int count,char **value)
{
for(int i=0;i<count;i++)
if(*value[i] == NULL)
return false;
else cout<<*value<<endl;
}
void main()
{
char *p[5];
for(int i=0;i<5;i++)
{
p[i]=new char[10];
sprintf(p[i],"hello");
}
display(5,p);
//free memory
for(i=0;i<5;i++)
{
delete[] p[i];
}
}
在数组前面加上* 表示这个是一个数组指针,
给你举个列子
#include <iostream>
#include <time.h>
using namespace std;
/*
this is a function to dispaly array of string
@count is the number of strings
@value is the first address of the first string
*/
bool display(int count,char *value[])
{
for(int i=0;i<count;i++)
if(*value[i] == NULL)
return false;
else cout<<*value<<endl;
}
void main()
{
char *p[5];
for(int i=0;i<5;i++)
{
p[i]=new char[10];
sprintf(p[i],"hello");
}
display(5,p);
//free memory
for(i=0;i<5;i++)
{
delete[] p[i];
}
}