#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;
}
C/C++内存指针的使用
最新推荐文章于 2025-12-24 12:31:41 发布
2506

被折叠的 条评论
为什么被折叠?



