指針基礎1

1 #include
  2 #include<string.h>
  3 
  4 int main()
  5 {
  6         char a[100] = {'h','e','l','\0','w'};
  7         "csd1007";//const char[8]
  8         puts(a);
  9         puts("csd1007");
 10         char *p = a;
 11         printf("%c\n",*p);
 12         *p = 'w';
 13         puts(a);
 14         p = "csd1007";//編譯器可能有警告
 15         printf("%c\n",*p);
 16         //*p = 'w';//編譯通過,運行段錯誤
 17         const char* q = NULL;//把指針指向的對象當作變量 = char const *q
 18         q = "csd1007";//一定安全
 19         //*q = 'w';編譯就出錯
 20         p = a;
 21         strcpy(a,"NB");
 22         puts(p);
 23         q = a+4;

 24         puts(q);

25 
 26         char* const r = a;//指針常量
 27         puts(r);
 28         //r = a;編譯錯誤
 29         *r = 'A';
 30         puts(a);
 31 
 32         char * str = NULL;
 33         scanf("%s",str);
 34         strcpy(str,"hello");
 35 
 36         return 0;
 37 }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章