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:  }
  • 相关阅读:
    (三十九)golang--反序列化
    (三十八)golang--json(对切片、map、结构体进行序列化)
    (三十七)golang--如何获取命令行参数
    (三十六)golang--文件的基本操作
    golang实战--客户信息管理系统
    Linux系统的LOG日志文件及入侵后日志的清除
    Linux下清除系统日志方法
    linux log日志解析
    linux添加开机自启动脚本示例详解
    linux添加开机启动项的方法介绍
  • 原文地址:https://www.cnblogs.com/justinzhang/p/2667214.html
Copyright © 2011-2022 走看看