基本上,我需要我的程序显示如下内容:http://i.imgur.com/ZOfnEJm.png
我觉得在很大程度上,我的代码已经失效了。但是,我在代码的||
部分之间显示任何内容时遇到问题。编译时,两者之间没有任何内容。 '格式'应该是这样的:
the memory address is on the far left, followed by two groups of eight-byte sequences (each byte is represented with a two-digit hex value), followed by the ASCII representation of the hex bytes placed between the vertical bars on the far right
。如果十六进制代码不是ASCII字符,我将显示一个句点(。)。
转到代码,然后:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
void DumpMem(void *arrayPtr, int numBytes);
int main()
{
auto int numBytes;
auto double *doublePtr;
auto char *charPtr;
auto int *intPtr;
// Doubles
printf("How many doubles? ");
scanf("%d", &numBytes);
doublePtr = malloc(numBytes * sizeof(doublePtr));
if (NULL == doublePtr)
{
printf("Malloc failed!");
}
printf("Here's a dynamic array of doubles... \n");
DumpMem(doublePtr, numBytes);
free(doublePtr);
// Chars
printf("\nHow many chars? \n");
scanf("%d", &numBytes);
charPtr = malloc(numBytes * sizeof(charPtr));
if (NULL == charPtr)
{
printf("Malloc failed!");
}
printf("Here's a dynamic array of chars... \n");
DumpMem(charPtr, numBytes);
free(charPtr);
// Ints
printf("\nHow many ints? \n");
scanf("%d", &numBytes);
intPtr = malloc(numBytes * sizeof(intPtr));
if (NULL == intPtr)
{
printf("Malloc failed!");
}
printf("Here's a dynamic array of ints... \n");
DumpMem(intPtr, numBytes);
free(intPtr);
}
void DumpMem(void *arrayPtr, int numBytes)
{
auto unsigned char *startPtr = arrayPtr;
auto unsigned char *ptrOne = startPtr;
auto unsigned char *endPtr = arrayPtr + numBytes;
auto int counter = 0;
printf("%p ", &startPtr);
for (; startPtr < endPtr; startPtr++)
{
printf("%02x ", *startPtr);
counter++;
if (counter == 16)
{
printf("|");
for(; ptrOne < 16; ptrOne++)
{
if (isalpha(*ptrOne))
{
printf("%c", *ptrOne);
}
else if (isdigit(*ptrOne))
{
printf("%d", *ptrOne);
}
else if (ispunct(*ptrOne))
{
printf("%c", *ptrOne);
}
else
{
printf(".");
}
}
printf("|");
printf("\n");
printf("%p ", &startPtr);
counter = 0;
}
}
}
那么,想法?求救!
答案 0 :(得分:0)
这条线似乎是你的问题。您正在比较指向整数的指针,并且该指针几乎肯定会大于16,因此您永远不会进入循环来打印任何内容。
for (; ptrOne < 16; ptrOne++)
然而,这样的东西会打印出来。
int i;
for (i = 0; i < 16; i++, ptrOne++)