Thread: Assigning an address to a global variable.

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    19

    Question Assigning an address to a global variable.

    Is there a way to assign an address to a non-pointer global variable???

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Well sure. An address is just a number:
    Code:
    unsigned long foo = 0x12345678;
    
    void *ptr = 0x12345678;
    What exactly are you trying to do?????!!!!!OMFGBBQ
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    19
    unsigned long foo = 0x12345678;

    Just sets foo.

    I am trying to set the address of foo to 0x12345678

    Is there a way to do that without making foo a pointer?

  4. #4
    Dump Truck Internet valis's Avatar
    Join Date
    Jul 2005
    Posts
    357
    Code:
    someVar = (someVar's type) &someOtherVar
    Be careful because you might truncate the address depending on what the code is compiled on.

    edit; I should have more carefully read your reply, you can't change the address of a variable because it's either a relative address on the stack that the compiler works with itself or some offset in a data section in your built application. Maybe with some wierd pragma or attribute you could set the address but I can't think of a reason you would want to. What are you trying to do in a broader sense (say, the entire function or application)?
    Last edited by valis; 08-11-2006 at 12:26 PM.

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    I don't think there's a way to change the address of foo by even making it a pointer. You shouldn't care where variables are placed in memory unless you're doing something suspicious.
    If you understand what you're doing, you're not learning anything.

  6. #6
    Registered User
    Join Date
    Aug 2006
    Posts
    19
    The code is compiled on a Unix Machine.

    When trying the code you suggested I get the following error.

    "Conversion to non-scalar type requested"

  7. #7
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    Why would you want to do this? You can not make up an address to assign a variable to. You can make a pointer and then make it point to an address. If you could pick the address off a variable, what is to make sure you are not overwriting other variables, or doing malicious stuff.
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  8. #8
    Registered User
    Join Date
    Aug 2006
    Posts
    19
    I have an array of structures. I need to allocate fixed memory space, so that I can manipulate this array of structures, which needs to be in this fixed mem space.
    I think that making a pointer to a structure type, and then setting that pointer to point to this space is probably the best way to go.

  9. #9
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Code:
    struct foo *structptr = (struct foo *)fixed_memory_ptr;
    If you understand what you're doing, you're not learning anything.

  10. #10
    Registered User
    Join Date
    Aug 2006
    Posts
    19
    Thank you!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. controlling modifying global variable
    By sunny_master_07 in forum C Programming
    Replies: 9
    Last Post: 04-11-2008, 04:30 AM
  3. help with fgets and assigning line to variable
    By pabl_o in forum C Programming
    Replies: 5
    Last Post: 04-11-2005, 09:20 PM
  4. global variable turns zero
    By Roaring_Tiger in forum C Programming
    Replies: 5
    Last Post: 04-07-2003, 12:52 PM
  5. Problam with a global class variable
    By WebmasterMattD in forum C++ Programming
    Replies: 11
    Last Post: 01-21-2003, 04:38 AM