定义指针实现字符串逆序
运行结果及报错内容
#include<stdio.h>
void f(char* p);
int main(){
char st[20];
scanf("%s",st);
f(st);
printf("%s",st);
}
void f(char* p){
char* p1=p;
char* p2=p;
int count=0;
while((*p1)!='\0'){
p1++;
count++;
}
int i,j;
for(i=0;i<count;i++){
for(j=0;j<count-1-i;j++){
*p2=*(p+j);
*(p+j)=*(p+j+1);
*(p+j+1)=*p2;
}
}
}
我的解答思路和尝试过的方法
#include<stdio.h>
void f(char* p);
int main(){
char st[20];
scanf("%s",st);
f(st);
printf("%s",st);
}
void f(char* p){
char* p1=p;
char var; //char* p2=p;
int count=0;
while((*p1)!='\0'){
p1++;
count++;
}
int i,j;
for(i=0;i<count;i++){
for(j=0;j<count-1-i;j++){
var=*(p+j); //*p2=*(p+j)
*(p+j)=*(p+j+1);
*(p+j+1)=var;//*(p+j+1)=*p2
}
}
}
为什么用指针来存储无法实现呢?