C/C++内存指针的使用

#include <iostream>
#include "vector"
#include "stdint.h"
#include "stdlib.h"
#include "string.h"
using namespace std;
void test(){

    std::cout << "Hello, World!" << std::endl;

    char data[10] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'};
    // 输出 data 数组的地址
    std::cout << "data address: " << std::hex << std::showbase << static_cast<void*>(data) << std::endl;

    for (int i = 0; i < 10; ++i) {
        // 输出 data 数组每个元素的地址
        std::cout << "data[" << i << "] address: " << std::hex << std::showbase << reinterpret_cast<void*>(&data[i]) << std::endl;
    }

    uint16_t num[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    // 输出 num 数组的地址
    std::cout << "num address: " << std::hex << std::showbase << reinterpret_cast<void*>(num) << std::endl;
    for (int i = 0; i < 10; ++i) {
        // 输出 num 数组每个元素的地址
        std::cout << "num[" << i << "] address: " << std::hex << std::showbase << reinterpret_cast<void*>(&num[i]) << std::endl;
    }

    // 输出 data 数组占用的字节数
    std::cout << "sizeof char: " << std::dec << sizeof(data) << std::endl;
    // 输出 num 数组占用的字节数
    std::cout << "sizeof uint16: " << std::dec << sizeof(num) << std::endl;
    // 输出 data 数组的元素个数
    std::cout << "char circle count: " << std::dec << sizeof(data) / sizeof(data[0]) << std::endl;
    // 输出 num 数组的元素个数
    std::cout << "num circle count: " << std::dec << sizeof(num) / sizeof(num[0]) << std::endl;


}

void test2(){
    printf("function line: %s  column: %d \r\n",__FUNCTION__ ,__LINE__ );
    vector<uint16_t>data={11,12,13,14,15,16,17,18,1999};

    for (int i = 0; i < data.size(); ++i) {
        printf("data[%d] address:%p\r\n",i,(void*)&data[i]);
    }
    uint16_t * pointer=&data[0];
    uint8_t *bytePtr= reinterpret_cast<uint8_t *>(pointer);
    uint8_t *bytePtr1=reinterpret_cast<uint8_t*>(bytePtr+1);
    printf("address:%p  value:%d \r\n",bytePtr,*bytePtr);
    printf("address:%p  value:%d \r\n",bytePtr1,*bytePtr1);

//    int byteDataSize=data.size()*sizeof(int16_t);
//    uint8_t byteData[byteDataSize];
//    memcpy(byteData,data.data(),byteDataSize);
//    for (int i = 0; i < byteDataSize; ++i) {
//        printf("%02X  ",byteData[i]);
//    }
    printf("\r\n");
}

void test3(){
    std::vector<uint16_t> data = {11, 12, 13, 14, 15};

    // 假设我们想查看第 2 个元素(索引为 1)的地址及其下一个字节对应的数据
    uint16_t* targetPtr = &data[1];
    std::cout << "Target address: " << targetPtr << std::endl;

    // 打印目标地址对应的 uint16_t 数
    std::cout << "Value at address " << targetPtr << ": " << *targetPtr << std::endl;

    // 将地址转换为 uint8_t* 类型,以便逐个字节访问
    uint8_t* bytePtr = reinterpret_cast<uint8_t*>(targetPtr);

    // 打印目标地址及其下一个地址对应的字节值
    std::cout << "Value at address " << static_cast<void*>(bytePtr) << ": "
              << static_cast<int>(*bytePtr) << std::endl;
    std::cout << "Value at address " << static_cast<void*>(bytePtr + 1) << ": "
              << static_cast<int>(*(bytePtr + 1)) << std::endl;

}
int main() {
    
    std::cout << "Hello, World!" << std::endl;

    char data[10]={'a','b','c','d','e','f','g','h','i','j'};
    printf("data address:%p\r\n",(void*)data);

    for (int i = 0; i < 10; ++i) {
        printf("data[%d] address:%p\r\n",i,(void*)&data[i]);
    }

    uint16_t num[10]={1,2,3,4,5,6,7,8,9,10};
    printf("num address:%p \r\n",(void*)num);
    for (int i = 0; i < 10; ++i) {
        printf("num[%d] address:%p\r\n",i,(void*)&num[i]);
    }

    printf("sizeof char:%d\r\n",sizeof(data));
    printf("sizeof uint16:%d\r\n",sizeof(num));
    printf("char circle count:%d\r\n",sizeof(data)/sizeof(data[0]));
    printf("num circle count:%d\r\n",sizeof(num)/sizeof(num[0]));



    int num1 = 255;

    // 输出十进制数,showbase 对十进制无影响
    std::cout << "Decimal (with showbase): " << std::showbase << std::dec << num1 << std::endl;
    std::cout << "Decimal (without showbase): " << std::noshowbase << std::dec << num1 << std::endl;

    // 输出八进制数
    std::cout << "Octal (with showbase): " << std::showbase << std::oct << num1 << std::endl;
    std::cout << "Octal (without showbase): " << std::noshowbase << std::oct << num1 << std::endl;

    // 输出十六进制数
    std::cout << "Hexadecimal (with showbase): " << std::showbase << std::hex << num1 << std::endl;
    std::cout << "Hexadecimal (without showbase): " << std::noshowbase << std::hex << num1 << std::endl;

    
    //test();
    test2();
    //test3();
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值