![]() |
#2
无名可用2010-08-27 00:19
|

//char 型指针变量的特点
#include<iostream>
using namespace std;
int main( )
{
char * pstr[] = { "Robert Redford", //数组未提供参数
"Hopalong Cassidy",
"Lassie",
"Slim Pickens",
"Boris Karloff",
"Oliver Hardy",
"Huang MingSheng",
"Wgh miss"
}; //声明指针数组,并赋初值,注意有分号
int count = (sizeof pstr) / ( sizeof pstr[0]);
char * pstart = "Your lucky star is " ; //声明一个 char 型指针,并赋初值 这是 char 型指针变量的特点
int dice = 0;
cout << endl
<< " Pick a lucky star!" << endl
<< " Enter a number between 1 and " << count << ": ";
cin >> dice; //输入数据
cout << endl;
if(dice >= 1 && dice <= count) //如果 1 <= dice <= count
cout << pstart << pstr[dice - 1]; //这里第一个输出 pstart ,意味着输出的是字符串,这是 char 型指针的特点
else
cout << "Sorry, you haven't got a lucky star.";
cout << endl;
cout << "The dice bit number is: " << sizeof dice << endl; // 变量 dice 占用的内存字节数
cout << "Number of array element is: " << (sizeof pstr) / ( sizeof pstr[0]) << endl; //输出数组元素的数量
system("pause");
return 0;
}
[ 本帖最后由 hmsabc 于 2010-8-26 19:18 编辑 ]