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:  }
  • 相关阅读:
    在JavaWeb中使用Log4j步骤
    关于我为什么不再更新博客园了
    windows termial 配置文件
    windows10 命令行修复系统引导
    vscode修改code runner插件默认使用的编译器
    windows下vscode修复c++找不到头文件
    windows下安装mingw-w64
    vscode美化方法以及定制主题插件
    windows下隐藏磁盘分区
    2018 icpc 徐州网络赛 F Features Track
  • 原文地址:https://www.cnblogs.com/justinzhang/p/2667214.html
Copyright © 2011-2022 走看看