请问关于win-tc里指针的用法。

来源:百度知道 编辑:UC知道 时间:2024/06/30 01:26:31
刚刚下得win-tc,试着编了个指针程序。
因用函数的时候出错,好人们哪,帮忙看看吧~
#include "stdio.h"
#include "conio.h"
#define N 80
main()
{
char a[N];
printf("enter:");
gets(a);
printf("the string is:");
puts(a);
chg(a);
printf("the string is:");
puts(a);
getch();
}
void chg(char *s)
{
while(*s)
if(*s=='z'||*s=='Z')
{*s-=25;s++;}
else if(*s>='a'&&*s<='z')
{*s++;s++;}
else if(*s>='A'&&*s<='Z')
{*s++;s++;}
else
s++;
}
它总是说错误 与'chg'声明中的类型不匹配,哭ing~~~
哪,该怎么改呀??

首先chg函数的原形没有声明,在main()的语句上面加一句void chg(char *s);声明一下函数。

其次修改void chg(char *s)函数体,我给你重新写了个。
void chg( char *s ) {
char *p = s; /* 为了不让s指针改变,所以复制一下 */
while( *p != '\0' ) { /* 当*p还没有达到字符串尾部 */
if( *p >= 'A' && *p <= 'Z') /* 若是大写字母 */
*p = (*p - 'A' + 1) % 26 + 'A'; /* *p - 'A'就是将原本在A~Z区间的Ascii吗转换到0~25之间,然后+1就是下一个字符,在%26是查看*p-'A'+1是否达到26也就是到Z,取余结果为0则达到'Z',也就是0+'A'就是'A'吧。 */
if( *p >= 'a' && *p <= 'z') /* 若是小写字母 */
*p = (*p - 'a' + 1) % 26 + 'a';
p++;
}

全部程序:
#include "stdio.h"
#include "conio.h"
#define N 80
void chg(char *s);
main()
{
char a[N];
printf("enter:");
gets(a);
printf("the string is:");
puts(a);
chg(a);
printf("the string is:");
puts(a);
getch();