1

したがって、基本的には、プログラムに次のように表示させたい:
(メモリ アドレス) (16 バイトの 16 進値) (文字単位の 16 進値)
これで、次の行が常に返さ'0'れるため、文字が表示されないことを除いて、正しい形式になりました。 at all:
printf("%c", isgraph(*startPtr)? *startPtr:'.');
最後に、私はsrandand をrand正しく使用していると思いますが、配列がランダムなものでいっぱいになっていません。いつも同じです。
とにかく、ここにコードがあります:

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <time.h>

void DumpMem(void *arrayPtr, int numBytes);
void FillMem(void *base, int numBytes);

int main(void)
{
    auto int numBytes;
    auto double *doublePtr;
    auto char *charPtr;
    auto int *intPtr;

    srand(time(NULL));
    // 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");
    FillMem(doublePtr, numBytes * sizeof(*doublePtr));
    DumpMem(doublePtr, numBytes * sizeof(*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");
    FillMem(charPtr, numBytes * sizeof(*charPtr));
    DumpMem(charPtr, numBytes * sizeof(*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");
    FillMem(intPtr, numBytes * sizeof(*intPtr));
    DumpMem(intPtr, numBytes * sizeof(*intPtr));

    // Free memory used
    free(doublePtr);
    free(charPtr);
    free(intPtr);
}

void DumpMem(void *arrayPtr, int numBytes)
{
    auto unsigned char *startPtr = arrayPtr;
    auto int counter = 0;
    auto int asciiBytes = numBytes;

    while (numBytes > 0)
    {
        printf("%p ", startPtr);

        for (counter = 0; counter < 8; counter++)
        {
            if (numBytes > 0)
            {
                printf("%02x ", *startPtr);
                startPtr++;
                numBytes--;
            }
            else
            {
                printf("   ");
            }
        }

        printf(" ");

        for (counter = 0; counter < 8; counter++)
        {
            if (numBytes > 0)
            {
                printf("%02x ", *startPtr);
                startPtr++;
                numBytes--;
            }
            else
            {
                printf("   ");
            }
        }

        printf(" |");
        // 'Rewind' where it's pointing to
        startPtr -= 16;

        for (counter = 0; counter < 16; counter++)
        {
            if (asciiBytes > 0)
            {
                printf("%c", isgraph(*startPtr)? *startPtr:'.');
                asciiBytes--;
            }
            else
            {
                printf(" ");
            }
        }
        puts("| ");
    }
}

void FillMem(void *base, int numBytes)
{
    auto unsigned char *startingPtr = base;

    while (numBytes > 0)
    {
        *startingPtr = (unsigned char)rand;
        numBytes--;
        startingPtr++;
    }
}

配列内でランダムな値を取得できないのはなぜですか? そして、なぜ私の条件文は常に'false'?

4

1 に答える 1

1

rand乱数ではなく、への関数ポインターの下位バイトを配列に入力しています。関数を呼び出す必要があります。

    *startingPtr = (unsigned char)rand();

startPtrまた、文字データの印刷中にインクリメントしていません。あなたstartPtr++はそこに必要です:

        if (asciiBytes > 0)
        {
            printf("%c", isgraph(*startPtr)? *startPtr:'.');
            startPtr++;
            asciiBytes--;
        }

現状では、プログラムは最初のバイトを何度も印刷し、次の行に進み、前の行と同じものを印刷します。

于 2013-10-16T23:09:08.267 に答える