zoukankan      html  css  js  c++  java
  • 指针

    int main()
    {
    
    
        int* p;
       //这表明*p 类型为int, 由于*被用于指针,因此p变量本身必须为指针
       //我们说p 是指向int类型,可以这样说p 为指针(地址),而*p是int而不是指针
        int a[3] = {1, 2, 3};
        p = &a[0];
        cout<< "p: " << p << "*p :"<< *p <<endl;
        p++;
        cout<< "p: " << p << "*p :"<< *p <<endl;
        int * p1 = a;
        //or
         p1 = &a[0];
        // p1 默认为数组第一个元素的地址,同样也可以将数组名作为指针,通过p1++的方式进行访问元素
    
        cout << *++p1 << endl;
       
    //一定要在解除引用运算符(*)之前,将指针初始化为一个确定的、适当的地址
        //例如
        int* a = 123;
        //直接访问会报错误
        printf("%d", *a);
        //对于c中的字符串其实内存中连续的字符数组,跟数组类似
        char c[] = "12131";
        char *p;
        p = c;
        //等价于
        char * p = "12131";
        //数组名默认为数组的首地址 等价于&c[0]
        //指针默认指向的字符串位于内存的常量区,并且不可以修改
        //默认初始化则是在栈区
    
    
    

      

      // 第四章 课后练习 //普通数组 char actor[30] = "sdfsdf"; string str = "sdfsdf"; //动态数组 vector<int> arr1(2); //分配在堆 array<int ,4> arr2 = {1, 2, 3, 5}; //分配在栈 cout<< arr2.at(0) << endl; double arr3[10] = {1.0, 2.0, 3.0, 4.0}; double* t = arr3; cout << *t << endl; t = t + 3; cout << *t << endl; int tmp; cin >> tmp; int *arr4 = new int[tmp]; cout << sizeof arr4 << endl; delete arr4; const int Max = 10; vector<string> tmp1(Max); array<string, Max> tmp2; return 0; }
  • 相关阅读:
    Python元组、列表、字典
    测试通过Word直接发布博文
    Python环境搭建(windows)
    hdu 4003 Find Metal Mineral 树形DP
    poj 1986 Distance Queries LCA
    poj 1470 Closest Common Ancestors LCA
    poj 1330 Nearest Common Ancestors LCA
    hdu 3046 Pleasant sheep and big big wolf 最小割
    poj 3281 Dining 最大流
    zoj 2760 How Many Shortest Path 最大流
  • 原文地址:https://www.cnblogs.com/alplf123/p/7993261.html
Copyright © 2011-2022 走看看