如何将指针初始化到在C中的特定存储器地址++ [英] How to initialize a pointer to a specific memory address in C++

查看:133
本文介绍了如何将指针初始化到在C中的特定存储器地址++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
  指向特定的固定地址

这个一个有趣的讨论开始<一href=\"http://stackoverflow.com/questions/3933263/questions-every-good-c-c-developer-should-be-able-to-answer/3933402#3933402\">here但没有人已经能够提供这样的C ++方式:

An interesting discussion about this started here but no one have been able to provide the C++ way of doing:

#include <stdio.h>

int main(void)
{
  int* address = (int *)0x604769; 
  printf("Memory address is: 0x%p\n", address);

  *address = 0xdead; 
  printf("Content of the address is: 0x%p\n", *address);

  return 0;
}

什么是用C做这样的事++最合适的方式?

What is the most appropriate way of doing such a thing in C++?

推荐答案

在C ++中,总是preFER reinter pret_cast 通过C-演员。它是如此丑陋的屁股,有人会立即发现的危险。

In C++, always prefer reinterpret_cast over a C-cast. It's so butt ugly that someone will immediately spot the danger.

例如:

int* ptr = reinterpret_cast<int*>(0x12345678);

这件事伤害了我的眼睛,我喜欢它。

That thing hurts my eyes, and I like it.

这篇关于如何将指针初始化到在C中的特定存储器地址++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆