我在这里有几行:
#include <stdio.h>
char *tolower(char *data)
{
char *p = data;
while(*p)
{
printf("nilai p : %c\n",*p);
if(*p >= 'A' && *p <= 'Z')
{
*p += ('a' - 'A');
}
p++;
}
return p;
}
int main()
{
char *a = "HajAR BleH";
char *b = tolower(a);
printf("nilai b : %s\n",b);
printf("nilai a - A : %d\n",'a' - 'A');
return 0;
}
接下来,编译,在 gdb 上运行,并跟踪分段
xxx@aaa:/tmp$ gcc -o aa aa.c --debug
xxx@aaa:/tmp$ gdb -q aa
Reading symbols from /tmp/aa...done.
(gdb) r
Starting program: /tmp/aa
nilai p : H
Program received signal SIGSEGV, Segmentation fault.
0x0804841e in tolower (data=0x804855e "HajAR BleH") at aa.c:11
11 *p += ('a' - 'A');
(gdb)
问题
1.我认为*p += ('a' - 'A');
会等于'H' += ('a' - 'A')
和等于72 += 32
但是,不小心分段错误,怎么会这样?
2.为什么需要添加 ('a' - 'A') 来降低char
/byte
降低?
暂时就这些了,先谢谢了