book@100ask:~/C_coding/CH01$ cat charpointer.c
#include <stdio.h>
#include <stdlib.h>
int main()
{
char str[] = "I Love China!";
char *p = str+7;
puts(str);
puts(p);
exit(0);
}
book@100ask:~/C_coding/CH01$ make charpointer
cc charpointer.c -o charpointer
book@100ask:~/C_coding/CH01$ ./charpointer
I Love China!
China!
book@100ask:~/C_coding/CH01$ cat charpointer.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char str[] = "Hello";
//str = "World!";
strcpy(str,"World!");
puts(str);
exit(0);
}
book@100ask:~/C_coding/CH01$ make charpointer
cc charpointer.c -o charpointer
book@100ask:~/C_coding/CH01$ ./charpointer
World!
book@100ask:~/C_coding/CH01$ cat charpointer.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char str[] = "Hello";
printf("%d %d\n",sizeof(str),strlen(str)); //sizeof包括尾零;strlen不包括尾零
strcpy(str,"World!");
puts(str);
exit(0);
}
book@100ask:~/C_coding/CH01$ make charpointer
cc charpointer.c -o charpointer
book@100ask:~/C_coding/CH01$ ./charpointer
6 5
World!
book@100ask:~/C_coding/CH01$ cat charpointer.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char *str = "Hello";
printf("%d %d\n",sizeof(str),strlen(str));
strcpy(str,"World!"); // str指向常量"Hello",该常量不可用被"World!"赋值,因此报段错误
puts(str);
exit(0);
}
book@100ask:~/C_coding/CH01$ make charpointer
cc charpointer.c -o charpointer
book@100ask:~/C_coding/CH01$ ./charpointer
8 5
Segmentation fault (core dumped)
book@100ask:~/C_coding/CH01$ cat charpointer.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char *str = "Hello";
printf("%d %d\n",sizeof(str),strlen(str));
//strcpy(str,"World!");
str = "World!";
puts(str);
exit(0);
}
book@100ask:~/C_coding/CH01$ make charpointer
cc charpointer.c -o charpointer
book@100ask:~/C_coding/CH01$ ./charpointer
8 5
World!