-2

基本上,我需要我的程序显示如下内容: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;
        }
    }

}  

那么,想法?请帮忙!

4

1 回答 1

0

这条线似乎是你的问题。您正在将指针与整数进行比较,并且该指针几乎肯定会大于 16,因此您永远不会进入循环打印任何内容。

for (; ptrOne < 16; ptrOne++)

然而,像这样的东西会打印出来。

int i;

for (i = 0; i < 16; i++, ptrOne++)
于 2013-10-15T01:35:27.767 回答