当前位置: 首页 > article >正文

19.C++ 中将一维数组转成多维的三种方式

文章目录

    • 使用`reinterpret_cast`
    • 强制类型转换
    • 使用static_cast


欢迎访问个人网络日志🌹🌹知行空间🌹🌹


使用reinterpret_cast

reinterpret_cast作用为:允许将任何指针转换为任何其他指针类型。

#include <iostream>
int main() {
    int arr4[2] = {0, 12};
    int* p5 = reinterpret_cast<int*>(&arr4);
    cout << p5[1] << std::endl; 

    struct S1 { int a; } s1;
    s1.a  = 100;
    int* p1 = reinterpret_cast<int*>(&s1);
    cout << *p1;   
}

// 12
// 100

上面例子中,p1 是指向s1.a的指针,s1.as1是指针可互换的。

如何将一维指针转换成多维指针呢?看一个例子,

#include <iostream>

using namespace std;

int main()
{
    constexpr const unsigned long n = 8;
    float arr1[n] = {1.2f, 1.3f, 1.4f, 1.5f, 1.6f, 1.7f, 1.8f, 1.9f};
    float *arr = new float[n];
    for(int i = 0; i < n; i++) {
        arr[i] = arr1[i];
    }
    // 1.reinterpret_cast
    unsigned long I, J, K;
    I = 2;
    J = 2;
    K = 2;

    cout << arr[0] << endl;
    float (&arr3d)[I][J][K] = *reinterpret_cast<float (*)[I][J][K]>(arr);
    cout << arr3d[0][0][0] << endl;
}

float (*)[I][J][K]是数组指针类型,指向形如[I][J][K]数组的首地址,reinterpret_cast后将这个转换结果赋值给了arr3d

强制类型转换

float(*arr3d1)[I][J] = (float(*)[I][J])arr;
cout << arr3d1[0][0][0] << endl;

这里是将arr转换成float(*)[I][J]的数组指针,arr3d1相当于指向[I][J][K]数组的行指针[I][J][0],这里访问一定要知道arr指针中的元素个数,防止越界。

使用static_cast

static_cast操作符可用于将一个指向基类的指针转换为指向子类的指针。但是这样的转换不总是安全的。static_cast不够安全,就是指在运行阶段不进行类型检查。其主要作用是:

  • 基类(父类)和派生类(子类)之间指针或引用的转换
  • void指针转换成目标类型的指针
  • 数字类型转换,一般情况下,可以使用static_cast用于数字类型的转换,如把枚举转换为int或是 float类型。
float (&arr3d2)[I][J][K] = *static_cast<float (*)[I][J][K]>(static_cast<void*>(arr));
cout << arr3d2[0][0][0] << endl;

上面例子中先将float*转换成了void*,然后将void*转换成了float (*)[I][J][K]类型,再取指针变量的值作为数组arr3d2的引用,如此就完成了类型转换。看一个简单的例子:

float c[4] =  {1,2,3,4};
float (&b)[4] = *static_cast<float(*)[4]>(&c); 
cout<<b[1]<<endl;

上面例子b是一个数组变量的引用,float(*)[4]是指向数组的指针,*static_cast<float(*)[4]>是取指针的值,也就是数组的首地址,b正是这个首地址的引用。

转换成多维数组时,之所以需要先转成void*static_cast转换约束的限制,如果使用reinterpret_cast它可以强制性的将一个指针类型转换成另一种不会做检查,因此就不需要先转成void*

float c[4] =  {1,2,3,4};
float (&b)[2][2] = *static_cast<float(*)[2][2]>(static_cast<void*>(&c)); 
float (&d)[2][2] = *reinterpret_cast<float(*)[2][2]>(&c); 
cout<<b[1][1]<<endl;

从这里也能看出,static_cast类型转换是有保护的,如果同样能使用static_castreinterpret_cast时,应优先使用static_cast.


1.https://stackoverflow.com/questions/310451/should-i-use-static-cast-or-reinterpret-cast-when-casting-a-void-to-whatever


http://www.kler.cn/news/155180.html

相关文章:

  • CMake语法入门篇
  • 基于51单片机的交通灯_紧急开关+黄灯倒计时+可调时间
  • ASP.NET Core MVC过滤器
  • Spring如何解决循环依赖问题?
  • 【EasyExcel实践】导出多个sheet到多个excel文件,并压缩到一个zip文件
  • SmartSoftHelp8,数据库字段详细文档自动生成工具
  • LeetCode 每日一题 2023/11/27-2023/12/3
  • 【数电笔记】18-卡诺图化简
  • 【C++练级之路】【Lv.1】C++,启动!(命名空间,缺省参数,函数重载,引用,内联函数,auto,范围for,nullptr)
  • 锐捷RG-UAC应用网关 前台RCE漏洞复现
  • 说说你对Vue的理解
  • Pytorch中的Net.train()和 Net.eval()函数讲解
  • Java实战案例————ATM
  • 卫星影像数据查询网址(WORLDVIEW1/2/3/4、PLEIADES、SPOT系列、高景、高分1-7、资源系列、吉林一号等)
  • 【Unity动画】为一个动画片段添加事件Events
  • 深度学习——第03章 Python程序设计语言(3.1 Python语言基础)
  • 类和对象(上篇)
  • css中的 Grid 布局
  • 使用docker切换任意版本cuda使用GPU
  • wvp如果确认音频udp端口开放成功
  • 中断方式的数据接收2
  • 在 AlmaLinux9 上安装Oracle Database 23c
  • 回归预测 | MATLAB实现基于LightGBM算法的数据回归预测(多指标,多图)
  • 壹财基金杨振骏:资本如何做好Web3布局?
  • 整数转罗马数字算法(leetcode第12题)
  • 单片机第三季-第六课:STM32标准库
  • sql27(Leetcode1729求关注者的数量)
  • 国家数据局首次国考招聘12人
  • vue面试题整理(1.0)
  • 深入理解 Vue 中的指针操作(二)