2013-10-16 55 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