• 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:  }
  • 相关阅读:
    list容器的sort函数
    c++ vector迭代器删除元素
    vs中属性页常用配置介绍2
    vs中属性页常用配置介绍
    error 不是类或命名空间
    实习代码编写中,一些有用的经验
    vs2015环境下生动动态链接库及使用
    STL容器使用的时机
    c++文件和流
    c++接口(抽象类,虚函数,纯虚函数)
  • 原文地址:https://www.cnblogs.com/justinzhang/p/2667214.html
Copyright © 2020-2023  润新知