vc 中char* str和char str[]不一样么??

来源:百度知道 编辑:UC知道 时间:2024/05/14 17:07:17
#include <string.h>
#include <iostream.h>

template <class Stype>
void bubble(Stype* item,int count);

void main()
{

char str[]="gfedcba";
//char* str="gfedcba";
int n=strlen(str);
for(int j=0;j<n;j++)
cout<<str[j]<<" ";
cout<<endl;
bubble(str,n);
cout<<"the sorted strings are :"<<str<<endl;
int num[7]={2,3,45,4,6,12,33};
bubble(num,7);
cout<<"the sort numbers are:";
for(int i=0;i<7;i++)
cout<<num[i]<<" ";

}

template <class Stype>
void bubble(Stype *item,int count)
{
register int i,j;
Stype t;
for(i=1;i<count;i++)
for(j=count-1;j>=i;j--)
if (item[j-1]>item[j]) {
t=item[j-1];
item[j-1]=item[j];
item[j]=t;
}
}

这是肯定的, 因为你的 char* str='gfedcba'; 语句 系统是把它当作一个字符处理的,你输入的时候没有错。处理它就会出现错误。事实上 他是被系统当作常量字符处理了。

char str[]="gfedcba"; 与 char* str="gfedcba";在实质上是一样的,是中定义的方式不同!
你运行出错是因为你在后面的调用是调用的str[j], 如果改成*(str+j),还有后面的都要改