第1个回答 2011-02-27
对于char a; *a='x'; ,因为char a定义a是一个变量,而不是一个指针,你接着用还没有声明的 *a是不合法的,正确应该这样。char a = 'x'; char *b=&a;//表示指针变量b指向a所在的地址。
对于char *a[]={"abc","hij","opq"};实际上是一个指针数组,a[0]、a[1]、a[2]都表示指针变量。其中a[0]指向字符串"abc"的地址,a[1]指向"hij"的地址,a[2]指向"opq"的地址,所以是就要吧直接进行赋值,因为他们都有所指。。希望你能明白 。。。
第2个回答 2011-02-20
char* a="x"这个也可以直接赋值啊,同理char *a[]={"abc","hij","opq"};也就可以啊。相当于char* a[0]="abc", char* a[1] = "hij", char* a[2] = "opq".指针就是用来指向保存变量值的地址。
上面那个字符串的例子是这些常量字符串系统会为他们为配地址,然后将地址的值存在char*字符串中。如果是整型,浮点型之类的,你得先有一个变量声明,然后才能有指针指向它的地址啊
第3个回答 2011-02-20
char *a[]={"abc","hij","opq"};这样赋值当然是可以的啊。
首先你要明白"abc","hij","opq"都是字符串常量,在内存中存放在常量区。这三个常量的地址在我机子依次是:
0x00420f24
0x0042012c
0x0042001c
而a是一个元素为指针的数组,a[0],a[1],a[2]就分别指向上面三个地址。
追问若是变量,还能这样赋值么?例如:
char *a[]={'h','m','n'};
这样赋值的话合法么?怎样区分单个字符是变量还是常量?
追答首先h' ,'m' 和 'n'不是变量,他们都是const char类型(就是常量字符)
这样赋值是不合法的,编译器会报这样的错:
cannot convert from const char to char *
单个字符肯定是常量啊。
比如
char ch='a';
ch是变量,'a'是常量
关于常量和变量的概念建议多看看C或C++教材。
本回答被提问者采纳
第4个回答 2011-02-21
#include <iostream>
#include <string>
using namespace std;
template<class T,class M>
class student
{
private:
T name[10];
M age;
static M length;
public:
student();
void input();
void dele();
void out_top();
void output();
};
template<class T,class M>
student<T,M>::student()
{
memset(&name,0,sizeof name);
age = 0;
}
template <class T,class M>
M student<T,M>::length = 0;
template<class T,class M>
void student<T,M>::input()
{
M i;
cout<<"Please input the length:"<<endl;
cin>>length;
cout<<"Please input the information:"<<endl;
for (i = 0; i < length; i++)
{
cin >> this[i].name >> this[i].age;
}
}
template<class T,class M>
void student<T,M>::dele()
{
M i;
for (i = 1; i < length; i ++)
{
strcpy(this[i-1].name,this[i].name);
this[i-1].age = this[i].age;
}
length --;
}
template<class T,class M>
void student<T,M>::out_top()
{
cout<<"The top student's information:"<<endl;
cout<<"name:"<<this[0].name<<" "<<"age:"<<this[0].age<<endl;
}
template<class T,class M>
void student<T,M>::output()
{
M i;
for (i = 0; i < length; i ++)
{
cout<<"name:"<<this[i].name<<" "<<"age:"<<this[i].age<<endl;
}
}
int main()
{
student<char,int> *head;
head = new student<char,int>[10];
head->input();
head->out_top();
head->dele();
head->output();
}
我改了下,在我这里是能正常运行的。你试试吧。
你关键是看错了吧,把函数名看成是参数了。估计是从C转来C++的? :-)