char *s1 = "hello"和char s2[]="world"的区别
链接:http://www.voidcn.com/article/p-mionkggw-bkb.html
这两个字符串存储在一片连续的存储空间中,当时纳闷怎么一到*s1=*s2就挂了,查了一下原来是这样的,也即是说char *s1="aaaa"<=>const char * s1;代表(*s1)是个常量不能被改变。
所谓的指针是const 以及指针所指的值是const:
怎么分辨?
若是*在const 的前面表示指针是const,反之,则为指针所指的值为const.
同理,对于char *s[]={"boy","girl","mother","father"};这种定义里的字符串一定不能改变其中的任何字母
因为它等价于 char *(s+i)="dakhfkah";
char *s="boy",*s1="girl",*s2="mother",*s3="father".
也就是上面所说的char const *str。指针可以指向其他地方,但是指针所指的字符串字母不能够改变
例如:
不能strcpy(*st,s[1])!!!!理由上面已经说了
一下程序功能:在4个字符串中求最大字符串并输出结果
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include<string.h> 4 int main() 5 { 6 char *s[]={"boy","girl","mother","father"}; 7 char **st; 8 int i; 9 st=s; 10 for(i=1;i<4;i++) 11 if(strcmp(*st,s[i])<0) 12 *st=s[i]; 13 printf("%s\n",*st); 14 return 0; 15 }
运行结果如下:
至于为啥是mother,很简单因为mother的第一个字母'm'的ASCII都比其他几个字符串的第一个字母的ASCII大。