以十进制打印变量的地址 [英] Printing the addresses of variables in decimal

查看:103
本文介绍了以十进制打印变量的地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用十进制而不是十六进制打印数组元素的地址,但是它不起作用.下面是代码和输出示例.

I'm trying to print the addresses of the elments of an array in decimal instead of hexa but it doesn't work. Below is the code and output example.

#include <iostream>
#include <iomanip>
using namespace std;

void printarrandptr(int arr[], int size);
const int LEN = 5;

void main(){

    int arr[LEN] = { 15, 3, 14, 11, 14 };

    int *p[LEN];

    printarrandptr((int*)arr, LEN);
}

void printarrandptr(int arr[], int size){
    int i;

    for (i = 0; i < size; i++)
        cout << setw(9) << &arr[i] << setw(4) << arr[i] << endl;
    cout << endl;
}

输出示例:

 0098FDC8  15
 0098FDCC   3
 0098FDD0  14
 0098FDD4  11
 0098FDD8  14

推荐答案

ostream& operqator<<(ostream&, void*)的重载默认情况下使用十六进制格式.

There's a overload for ostream& operqator<<(ostream&, void*) that uses hex format by default.

cout << setw(9) << (long long)&arr[i] ...应该可以解决问题.

请不要讨论c风格的转换.

这篇关于以十进制打印变量的地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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