Thread: assigning a memory address to a pointer

  1. #1
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300

    assigning a memory address to a pointer

    Normally an address is assigned to a pointer this way:
    Code:
    int X, *ptr=&X;
    How can I assign the address from a string literal, Eg.

    Code:
    #define addr "0xbfe146bc"
    *ptr=addr;  //obviously not
    Such that this will now be the actual address of the pointer and usuable that way normally (if you need to know why, see
    http://cboard.cprogramming.com/showthread.php?t=107115
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    convert it to unsigned long using strtoul
    then, cast to pointer of the appropriate type
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by vart View Post
    convert it to unsigned long using strtoul
    then, cast to pointer of the appropriate type
    I presume that's what you were trying to do with your example in the other thread, but it doesn't work!
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by MK27 View Post
    I presume that's what you were trying to do with your example in the other thread, but it doesn't work!
    Works for me
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
    	char sAddr[] = "0x00ac0098";
    	unsigned int uAddr = strtoul(sAddr,NULL,0);
    	void* pAddr = (void*) uAddr;
    
    	printf("&#37;p", pAddr);
    	return 0;
    }
    it prints exactly the same address I have put into the string, of course dereferencing this address will fail - because it is just some random value - low chance to have something available there - put the address of something accessable - and you will have no problem dereferencing it...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by vart View Post
    Works for me
    Well, it is also somewhat different than your other code -- but thanks thanks thanks vart, that will do my just fine
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Why are you even using a string literal for this? I just noticed your other thread was using a string literal too. Why not just make it a constant?

    Code:
    #define addr 0x00ac0098
    It is much less overhead.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  2. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM
  3. Assigning memory address of member struct to pointer.
    By Tronic in forum C++ Programming
    Replies: 2
    Last Post: 03-20-2004, 05:53 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. size of the memory reserved for a pointer
    By cris_orpi in forum C Programming
    Replies: 13
    Last Post: 03-05-2003, 11:42 AM