Thread: Pointer variables

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    150

    Pointer variables

    I have a question about pointers. I want to take the address stored in a pointer and store it in an unsigned char... yeah, it's not big enough, i know, but it works for what im doing i think. Only problem is, everything I try yields the wrong result. I have two variables, ip and main_mem, both unsigned chars:

    Code:
    *ip = (unsigned char *) *main_mem + 1;
    and then I tried:

    Code:
    memcpy ((unsigned char *) ip, (unsigned char *) main_mem);
    So if this doesn't work, how do i do it? If more source is needed or if anyone wants an explanation, let me know. Thanks guys!!!
    HA! I WIN!

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Aside from it being a stupid thing to do, this should work:
    Code:
    unsigned char c;
    int *p;
    
    c = (unsigned char)p;
    You could also, if you want to use memcpy() do:
    Code:
    memcpy(&c, p, sizeof(unsigned char));
    However, depending on the byte-ordering (little endian or big endian) you may get "the wrong end" of the pointer that way.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting the matrix question..
    By transgalactic2 in forum C Programming
    Replies: 47
    Last Post: 12-22-2008, 03:17 PM
  2. Ban pointers or references on classes?
    By Elysia in forum C++ Programming
    Replies: 89
    Last Post: 10-30-2007, 03:20 AM
  3. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 02:19 AM
  4. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM
  5. ? about pointer variables?
    By correlcj in forum C Programming
    Replies: 4
    Last Post: 08-03-2002, 04:23 PM