Re: Convert Int Value To Pointer



On Fri, 14 Jul 2006 19:37:20 GMT, Frederick Gotham
<fgothamNO@xxxxxxxx> wrote in comp.lang.c:

ReaperUnreal posted:

I've got a strange problem here. I need to take a value that I have
written down on a sticky-note here, and find what's at that address in
current memory. I've tried the following:

void *value = 0x00323c68;

but I get a typecasting error. If I do the following:

void *value = static_cast<void *>(0x00323c68);

will that get me what I desire?


How about something like the following?

#include <stddef.h>
#include <limits.h>
#include <stdio.h>

void PrintBits(void const * const mem, size_t amount_bytes)
{
char static str[CHAR_BIT + 1]; /* Auto null teminator */

unsigned char const *p = (unsigned char const *)mem;

do
{
unsigned char const byte_val = *p++;

char *pos = str;

unsigned char i = 1U << CHAR_BIT - 1;

do *pos++ = byte_val & i ? '1' : '0';
while(i >>= 1U);

Do you realize that there is absolutely no gain to adding the 'U'
suffix here?


printf("%s",str);

} while (--amount_bytes);
}

int main(void)
{
void * const p = (void*)0x00323c68;

PrintBits( p, 1 );
}

Do you actually this makes the behavior any less undefined? Do you do
realize that without a terminating '\n', there is no guarantee that
any output will appear?

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
.



Relevant Pages

  • Re: Convert Int Value To Pointer
    ... written down on a sticky-note here, and find what's at that address in ... current memory. ... void PrintBits(void const * const mem, ...
    (comp.lang.c)
  • Re: Convert Int Value To Pointer
    ... written down on a sticky-note here, and find what's at that address in ... current memory. ... void *value = 0x00323c68; ... Ioan - Ciprian Tandau ...
    (comp.lang.c)
  • Re: Function Pointers
    ... Frederick Gotham posted: ... void ComputePrimeNumbers(void (*Callback)) ... void Print(unsigned long const num) ...
    (comp.lang.c)
  • Re: Checking return values for errors, a matter of style?
    ... Frederick Gotham wrote: ... do most good programmers use")? ... void Assure(int const i) ...
    (comp.lang.c)
  • Assertion before definitions
    ... What's the canonical way to perform an assertion before the definition of any ... void AddFiveEachElement ... /* Let's pretend we initialise an array here: ... Frederick Gotham ...
    (comp.lang.c)