Pergunta

So basically I want my program to display the following:
(memory address) (16 bytes of hex values) (those hex values in characters)
Now, I have the format correctly, except the following line always returns '0' and thus no characters are being displayed at all:
printf("%c", isgraph(*startPtr)? *startPtr:'.');
Finally, I think I'm using srand and rand correctly, but my array isn't being filled up with random stuff. It's always the same.
Anyway, here's the code:

#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++;
    }
}

Why am I not getting random values inside the array? And why is my conditional statement always 'false'?

Foi útil?

Solução

You're filling your array with the low-order byte of the function pointer to rand, not with a random number. You need to call the function:

    *startingPtr = (unsigned char)rand();

You also aren't incrementing startPtr while printing out the character data. You need a startPtr++ in there:

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

As it stands, your program just prints the first byte over and over again, and then goes on to the next line and prints an identical one to the previous line.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top