c语言,关于pointer的问题,求解,THX

来源:百度知道 编辑:UC知道 时间:2024/09/21 00:43:48
#include <stdio.h>
int main(int argc,char **argv){
char hello[]={'h','e','l','l','o','\0'};
char *s;
int len;

s = hello; // s now points to hello[0]
// could be "s = &hello[0];"
len = 0;
while(s[len] != '\0'){
len++;
}
hello[0]='y';
char *hello2 = "hello";
hello[0]='y'; //impossible

printf("len = %d\n",len);
printf("hello2[0] = %c\n",hello2[0]);
printf("hello[0] = %c\n", hello[0]);
printf("*hello = %c\n", *hello);
printf("s = %c\n",s[3]);
printf("s - hello = %d\n", s - hello);

char *c,*d;
*c = s[0];
*d = s[3];
printf("c = %c; c + 1 = %c\n",*c,*c + 1);
printf("d = %c; d - c = %d\n",*d,d-c);

原程序修改如下:
//---------------------------------------------------------------------------
#include <stdio.h>
int main(int argc,char **argv){
char hello[]={'h','e','l','l','o','\0'};
char *s;
char *hello2 = "hello";/*注意这里*/
char *c,*d;/*注意这里,C语言中,变量的定义一定要放在执行语句之前。*/
int len;

s = hello; // s now points to hello[0]
// could be "s = &hello[0];"
len = 0;
while(s[len] != '\0'){
len++;
}
hello[0]='y';

hello[0]='y'; //impossible

printf("len = %d\n",len);
printf("hello2[0] = %c\n",hello2[0]);
printf("hello[0] = %c\n", hello[0]);
printf("*hello = %c\n", *hello);
printf("s = %c\n",s[3]);
printf("s - hello = %d\n", s - hello);

c = &s[0];/*注意这里*/