能否幫我解決以下C問題?我試圖打印一個30個連續值的列表,從某個內存地址開始。我想打印1個字節作爲每個內存位置的十六進制。作爲插入哪個存儲器地址的指示,我在程序開始時打印虛擬地址。將內存打印爲C中的1個字節十六進制
問題是我得到的值超過1個字節。對於長序列的零,這仍然會導致00,但一旦非零值出現,我會打印一個4字節的「窗口」。這導致下面的輸出:
Main function address is 0x8048494
Dummy variable address is 0x9a2e008
Enter the address (without the 0x):9a2e008
You entered: 9a2e008
Address Byte value
0x9a2e008 00
0x9a2e009 00
0x9a2e00a 00
0x9a2e00b 00
0x9a2e00c 00
0x9a2e00d 00
0x9a2e00e 00
0x9a2e00f 00
0x9a2e010 00
0x9a2e011 f1000000
0x9a2e012 ff10000
0x9a2e013 20ff100
0x9a2e014 20ff1
0x9a2e015 3900020f
0x9a2e016 61390002
0x9a2e017 32613900
0x9a2e018 65326139
0x9a2e019 30653261
0x9a2e01a 30306532
0x9a2e01b 38303065
0x9a2e01c 383030
0x9a2e01d 3830
0x9a2e01e 38
0x9a2e01f 00
0x9a2e020 00
0x9a2e021 00
0x9a2e022 00
0x9a2e023 00
0x9a2e024 00
0x9a2e025 00
到目前爲止我的代碼是:
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#define NUMBER_OF_BYTES 10
void showAddresses(void);
void printMemoryAtAddress(void);
int * dummy;
int dumpSize = 30;
int main(void)
{
dummy = (int *) malloc (sizeof(int)); showAddresses();
printMemoryAtAddress();
return 0;
}
void showAddresses(void)
{
printf("Main function address is %p \n", main);
printf("Dummy variable address is %p \n",(void*)dummy);
}
void printMemoryAtAddress(void)
{
int input;
printf("Enter the address (without the 0x):");
scanf("%x", &input);
printf("You entered: %x \n", input);
printf("Address \tByte value \n");
int i;
for(i=0;i<dumpSize;i++)
{
int* address;
address = (int*) (input+i);
printf("%p \t", address);
printf("%.2x \n", *address);
}
}
這個問題的任何幫助將非常感謝!如果這個問題很愚蠢,我很抱歉。 (我仍然在學習,似乎無法找到的努力小時後的解決方案!) 喬
更改'int *地址;'爲'unsigned char *地址;' – wildplasser 2013-03-09 15:25:26
感謝您的快速回復!工作!順便說一句好的暱稱:) – Joe 2013-03-09 15:33:38