zoukankan      html  css  js  c++  java
  • 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:  }
  • 相关阅读:
    rest framework 认证 权限 频率
    rest framework 视图,路由
    rest framework 序列化
    10.3 Vue 路由系统
    10.4 Vue 父子传值
    10.2 Vue 环境安装
    10.1 ES6 的新增特性以及简单语法
    Django 跨域请求处理
    20190827 On Java8 第十四章 流式编程
    20190825 On Java8 第十三章 函数式编程
  • 原文地址:https://www.cnblogs.com/justinzhang/p/2667214.html
Copyright © 2011-2022 走看看