c++复杂数据类型——指针
发布日期:2021-05-15 08:56:37 浏览次数:24 分类:精选文章

本文共 2959 字,大约阅读时间需要 9 分钟。

指针存储和基本使用

int n = 100;	float value = 123.321;	cout << "n address = " << &n << endl;	cout << "value address = " << &value << endl;	long pointer = long(&value);	cout << "pointer = " << pointer << endl;	int* pn = &n;	float* pvalue = &value;	cout << "pn = " << pn << endl;	cout << "pvalue = " << pvalue << endl;	cout << "*pn = " << *pn << endl;	cout << "*pvalue = " << *pvalue << endl;	cout << sizeof(pn) << endl;	cout << sizeof(*pn) << endl;	cout << "*pvalue = " << *((float*)pointer) << endl;

在这里插入图片描述

指针移动和数据类型

int numbers[2] {   25,26};//    int *numbers;  numbers[0] numbers[1]    cout << "numbers[0]=" << *numbers << endl;    cout << "numbers[1]=" << *(numbers+1) << endl;        char chars[2] {   25, 26};    cout << "chars[0]=" << int(*chars) << endl;    cout << "chars[1]=" << int(*(chars+1)) << endl;        void *p = numbers;   // cout << "chars[1]=" << int(*(p+1)) << endl; // 报错,使用void* 接受无法分别指针类型,指针移动按照类型字节移动,int类型一次移动4个字节,指向下一个值

在这里插入图片描述

指针初始化

int xyz = 30;    int *p = &xyz ;    cout << "p address=" << p << endl;        p = &xyz;        *p = 123;        cout << "xyz = " << xyz << endl; // 123        xyz = 456;    cout << "*p = " << *p << endl; // 456

动态创建数组

int n = 20;    int *array3 = new int[n];     int *array4;    array4 = new int[100];        delete [] array4;        //array4 = NULL;  // 0    array4 = nullptr;  // c++ 11

字符串与指针

char str[10] = "hello";    cout << str << endl;    char *p = str;    cout << p << endl;        p = new char[strlen(str) + 1];    strcpy(p, str);        str[1] = 'x';    cout << str << endl;    *(p + 2) = 'w';    cout << p << endl;        char *p_str = "hello";  //  相当于指向常量字符的字符串指针,不能修改每一个字符    cout << p_str << endl;  //  *p_str = 's';        delete [] p;  //  delete [] p_str;    delete p_str;

常量指针和指针常量

const int code = 1234;  //  常量在定义时必须初始化	//    code = 3;  //  error    const int numbers[] = {   1,2,3,4};    //  numbers[2] = 44;  // error    	//  常量指针    const int *p = new int(100); // 常量指针通常在声明时初始化    cout << *p << endl;    // *p = 333;        char str[10] = "hello";    char const *p_str = str;  // 指向字符串的常量指针不允许修改字符    //*p_str = 'a';    p_str = new char[6];        //  指针常量    const char * const p_str1 = str; //指针常量不允许修改字符,也不允许重新分配内存   // *p_str1 = '1';    //p_str1 = new char[3];

用new创建结构体

struct MyStruct    {           int code;        char *name;        double price;    };    MyStruct *p = new MyStruct();    p->name = new char[30];    strcpy(p->name, "iPhone6 plus 256G");    (*p).code = 1234;    p->price = 9999;        cout << "p->name = " << p->name << endl; //iPhone6 plus 256G    cout << "(*p).code = " << (*p).code << endl; //1234    cout << "p->price = " << p->price << endl; //9999

用new创建共用体

union MyUnion    {           int code1;        long code2;    };        MyUnion *p = new MyUnion();    p->code1 = 200;    cout << "p->code1 = " << (*p).code1 << endl; //200    cout << "p->code2 = " << (*p).code2 << endl; // 200        (*p).code2 = 400;    cout << "p->code1 = " << p->code1 << endl; // 400
上一篇:c++复杂数据类型——动态分配(new)、 释放内存空间(delete)
下一篇:c++复杂数据类型——结构体、共用体、枚举、匿名类型、类型别名

发表评论

最新留言

第一次来,支持一个
[***.219.124.196]2025年04月25日 00时40分14秒