2013-10-16 15 views
1

所以基本上我想我的计划,显示如下:
(内存地址)(16个字节十六进制值)(以字符的十六进制值)
现在,我有格式正确,但下面的行总是返回'0',因此没有字符显示在所有:
printf("%c", isgraph(*startPtr)? *startPtr:'.');
最后,我认为我使用srandrand正确的,但我的数组没有被充满了随机的东西。它总是一样的。
总之,这里的代码:从十六进制显示为char使用条件语句

#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'

回答

1

您正在填充阵列,函数指针的低位字节为rand,而不是随机数。您需要通话功能:

*startingPtr = (unsigned char)rand(); 

您还没有增加startPtr同时打印出的字符数据。你需要一个startPtr++在那里:

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

既然这样,你的程序只是一遍又一遍打印的第一个字节一遍,然后继续到下一行并打印一个相同的前行。

+0

我觉得哑巴,哈哈。谢谢。但是,这并不会改变我没有在最后一列中显示字符的事实。 – relapsn

+0

@relapsn - 编辑来解决这个问题。 –

+0

哇,我不敢相信我忽略了这两件事。非常感谢! – relapsn