pointer and const

   1:  /*
   2:  author:justinzhang
   3:  email:uestczhangchao@gmail.com
   4:  time:2012-8-30 16:58:38
   5:  desc:for the practice of interview problem
   6:  */
   7:  #include <iostream>
   8:  using namespace std;
   9:  struct Foo
  10:  {
  11:      Foo(){}
  12:      Foo(int) {}
  13:      void fun() {}
  14:  };
  15:   
  16:  int main()
  17:  {
  18:      Foo a(10);
  19:      a.fun();
  20:      //Foo b();
  21:      //b.fun();
  22:   
  23:      const char *p1 = "hello";
  24:      char *const p2 = "world";
  25:      p1++;
  26:      //*p1 = 'w'; //error
  27:      //p2++;      // error
  28:      //*p2 = 'h';  //error : can not write to const string
  29:   
  30:      char str[][10] = {"hello","Google"};
  31:      char *pstr = str[0];
  32:      cout << strlen(pstr+10) << endl; 
  33:      /*
  34:  
  35:      str[ ][10]由定义可知,str[0]="Hello",str[1]="Google",
  36:  
  37:      内存中数据的存储为:
  38:  
  39:      str[0]                                       str[1]
  40:  
  41:      H e l l 0 '\0' '\0' '\0' '\0' '\0' '\0' G o o g l e '\0' '\0' '\0' '\0'
  42:  
  43:      所以p+10刚好为‘G’的地址,所以strlen(p+10)=6
  44:      */
  45:   
  46:      int x[4] = {0};
  47:      int y[4] = {1};
  48:   
  49:      for(int i=0; i<4; i++)
  50:          cout << x[i] << " " << y[i] << endl;
  51:   
  52:   
  53:      return 0;
  54:  }
posted @ 2012-09-01 23:41  justinzhang  阅读(281)  评论(0编辑  收藏  举报