Please diagnose my pointer problem


dear all,

i'm writing program project revolves around need dynamically allocate big chunk of memory. whatever reason, , despite searching, reading relevant chapters in books have, still unable easy setting value of char in memory allocated. example:

code:
int main(int argc, const char * argv[])  {        unsigned char* ptr;            ptr = calloc(1000, sizeof(char));            *ptr  = 0;      *(ptr+1) = 1;      *(ptr + 2) = 2;      *(ptr + 3) = 3;            printf("%c" , *(ptr + 2) );            return 0;  }    
this prints upside down question mark - not expected!

ideas on stupid thing i'm doing?

many thanks,

david
 

use %d instead of %c. %c prints variable character mapping ascii, ascii values in region not printable characters:

code:
       decimal set:           0 nul    1 soh    2 stx    3 etx    4 eot    5 enq    6 ack    7 bel         8 bs     9 ht    10 nl    11 vt    12 np    13 cr    14    15 si  
edit:

if intend use characters, need enclose values in single quotes:

code:
      *ptr  = '0';      *(ptr+1) = '1';      *(ptr + 2) = '2';      *(ptr + 3) = '3';            printf("%c" , *(ptr + 2) );  
 


Forums Macs Mac Programming


  • iPhone
  • Mac OS & System Software
  • iPad
  • Apple Watch
  • Notebooks
  • iTunes
  • Apple ID
  • iCloud
  • Desktop Computers
  • Apple Music
  • Professional Applications
  • iPod
  • iWork
  • Apple TV
  • iLife
  • Wireless

Comments

Popular posts from this blog

Carrier post Error specific

How to increase vRAM for HD3000 graphics

Carrier [Information] AT&T NEXT