指针输出字符串
#include <stdio.h>
#include <string.h>
int main(int argc, char const *argv[])
{
char s[99];
int i;
scanf("%s",s);
char *p=s;
printf("%s\n", p);
for (i = 0; i < strlen(s); ++i)
{
printf("%c,", *(p++));
}
printf("null%s\n", p);
//如果上面是*(p+i);那p就不会变。因为p=p+1;
//但还可以用s输出
printf("%s\n", s);
return 0;
}
拼接字符串
#include <stdio.h>
int main() {
char str1[20]={"Hello "}, str2[20]={"World"};
char *p=str1, *q=str2;
while( *p ) p++;
while( *q )
{
*p = *q;
p++;
q++;
}
*p = '\0';
printf("%s,%s\n", p,str1);
return 0;
}
总结:
在p指针指向数组时,可以看做str[1]=p[1],输出p+1和p++的结果可能一样,
但使用p+1这样,p是不回改变的,p++的话是p会改变的。
那输出原来p的话,就值不一样了,得输出回指向的str。