首页 > 编程语言 >C++数组类型中存入结构体类型对象

C++数组类型中存入结构体类型对象

时间:2023-03-22 11:23:41浏览次数:30  
标签:int uintptr unsigned C++ 存入 数组 类型 指针

最近看Binder机制的时候看到一个unsigned类型数组中不仅可以存放unsigned int类型还可以存放结构体对象,感到十分惊奇,下面我们来看一下

uintptr_t类型和指针类型的区别

uintptr_t是C/C++语言中一个无符号整数类型,它的长度和指针类型相同,在32位平台上为4字节,在64位平台上为8字节。uintptr_t通常用于不需要具体指针值但需要操作指针的场合,比如在某些底层系统编程中,需要用一个整数表示内存地址,而不是使用指针来操作内存。

指针类型则是一个用于存储变量地址的数据类型,例如int*表示指向int类型变量的指针。指针类型可以进行加减运算、解引用和等于/不等于比较等操作,因为指针类型存储了具体的内存地址。在程序中,指针类型通常用来动态分配内存,构建复杂数据结构等。

总的来说,uintptr_t类型是一个无符号整数类型,用于存储内存地址值;指针类型则是一种特殊的数据类型,用于存储变量的地址,并且支持各种针对指针的操作。在某些场景下,uintptr_t类型可以替代指针类型完成相应的操作,但是需要注意指针类型的生命周期和类型安全性问题。

示例

1、将unsigned类型数组转换为uintptr_t类型并打印出数组中的内容

#include <iostream>
#include <cstdint>

using namespace std;

int main() {
    unsigned arr[] = {1, 2, 3, 4, 5};
    int len = sizeof(arr) / sizeof(unsigned);

    uintptr_t addr = reinterpret_cast<uintptr_t>(arr);
    for (int i = 0; i < len; ++i) {
        uintptr_t ptr = addr + i * sizeof(unsigned);
        unsigned value = *reinterpret_cast<unsigned*>(ptr);
        cout << value << " ";
    }
    cout << endl;

    return 0;
}

使用了uintptr_t类型来存储数组的内存地址,并通过reinterpret_cast操作符将其转换为unsigned*类型的指针。然后,在循环中依次访问数组元素的内存地址,再通过reinterpret_cast将其转换为unsigned类型。

2、结构体类型存入unsigned类型的数组中并取出打印结构体信息

#include <iostream>

using namespace std;

struct Person {
    string name;
    int age;
    double height;
};

int main() {
    unsigned array[100];
    Person person = {"Li Ming", 25, 1.75};

    // 存储到数组中  只存储了一个对象  下面那个for循环遍历是在把结构体所有的字节都写到数组里面
    unsigned* ptr = reinterpret_cast<unsigned*>(&person);
    for (int i = 0; i < sizeof(Person) / sizeof(unsigned); ++i) {
        array[i] = *(ptr + i);
    }

    // 从数组中取出并打印结构体信息   如果还想要访问后面array数组的内容,可以array += (sizeof(*p)/sizeof(unsigned))
    Person* p = reinterpret_cast<Person*>(array);
    cout << "Name: " << p->name << endl;
    cout << "Age: " << p->age << endl;
    cout << "Height: " << p->height << endl;
  
    return 0;
}

在上面的例子中,我们定义了一个名为Person的结构体类型,包含三个成员变量:name、age和height。然后,我们创建了一个Person类型的变量person,并将其地址强制转换为unsigned类型的指针。接下来,我们使用循环把person所占用的内存块中的每个unsigned类型的数据存储到数组array中。最后,我们再次使用强制类型转换将数组中的unsigned类型数据转换为Person类型的指针,并打印出结构体信息。

 

除此之外,不仅仅可以使用unsigned类型数组、使用int类型等其他类型的数组都是可以的

 

标签:int,uintptr,unsigned,C++,存入,数组,类型,指针
From: https://www.cnblogs.com/kongbursi-2292702937/p/17243020.html

相关文章

  • C++ 字符串转16进制
    参考出处:https://blog.csdn.net/FinalCreed/article/details/71037420stringstring2hex(conststring&str){stringtemp;stringstreamss;stringresul......
  • C++查询ip归属地(QT)
    以查询ip归属地的例子来学习C++的API接口调用。常见的API接口可能会给出post及get两种访问方法,我们先学习较为简单的get方法。首先是找到相关的接口,如https://ip.userage......
  • 【转载】C++ 有用的资源
    https://www.runoob.com/cplusplus/cpp-useful-resources.html C++ 有用的资源以下资源包含了C++有关的网站、书籍和文章。请使用它们来进一步学习C++的知识。C+......
  • Redis - 基础数据类型
    简介根据官网文档的解释,可以了解Redis基础数据类型的一些基本信息:对于Redis来说,存储的key值都是字符串类型,讨论数据类型的时候,指的都是存储的value值。这里主......
  • 一统天下 flutter - dart: 数据类型(num, int, double, bool, String, List, Set, Map,
    一统天下flutterhttps://github.com/webabcd/flutter_demo作者webabcd一统天下flutter-dart:数据类型(num,int,double,bool,String,List,Set,Map,Object......
  • 自定义类型详解
    一、结构体在C语言中有int,char,float等等类型,可以用来形容某些数据,但是有些数据仅靠一种类型无法描述出来,比如说一个人,我们不仅要描述他的名字,还要描述他的身高、体重、性别......
  • c++链表记录
    ListNode*pre=NULL;//定义一个空节点ListNode*tmp;//定义一个空的临时节点,此时tmp==NULL ListNode*cur=head;//定义一个等于节点head的节点 ListNode*du......
  • C++ 树进阶系列之深度剖析字典(trie)树
    1.前文本文和大家一起聊聊字典树,从字典二字可知,于功能而言,字典树是类似于英汉字典的一棵信息树。字典树有2大特点:有容乃大。能存储大量的数据信息。提供有基于关键字......
  • Day04 - 判断数据类型的方式有哪些?| 面试365
    知识讲解​​JavaScript​​判断数据类型的方式共有四种typeofinstanceofconstructorObject.prototype.toStringtypeof​​typeof​​操作符返回一个字符串,表示操作值的......
  • algrothm_基本数据类型和引用类型变量
    ......