Thread: Howto assign an address to an integer variable

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    30

    Howto assign an address to an integer variable

    Hi,

    I would like to assign an address to an integer variable like in the following code but I am doing something wrong. Can someone tell me how this should be done correctly?

    Code:
    int main()
    {
    
    char string[] = "string";
    
    int x = &string;
    
    
    return 0;
    
    }
    Thank you

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    That won't work on 64-bit systems. The size of a pointer/address will be too large to fit into most ints in that case.

    Either use the corresponding pointer type like it was intended, or use some special type to hold it (ie. Either size_t or probably more appropriately ptrdiff_t, which appears to have its own potential issues.)

  3. #3
    Beautiful to C Aia's Avatar
    Join Date
    Jun 2007
    Posts
    124
    Code:
    int main()
    {
    
    char string[] = "string";
    
    int x = (int) &string;
    
    
    return 0;
    
    }
    When the eagles are silent, the parrots begin to jabber. ~Winston Churchill

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    As noted, it will only work on 32-bit systems with a cast to int. A cast to a 64-bit type, unsigned long long or unsigned __int64 can work.
    Remember that pointers are unsigned, as well.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    58
    It'll work on 16-bit systems too.

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    THIS (if we talk about casting pointer to int) will work on any system.

    The reverse casting will NOT always bring the original pointer value however.
    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

  7. #7
    Registered User
    Join Date
    Feb 2008
    Posts
    30
    Thanks all

    I don't understand why the casting has to take place. I would expect that
    Code:
    &string
    returns an address which could then be assigned to the variable. So why doesn't it work this way?

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Because only pointers hold an address. Other variables do not hold addresses (they can, but that's another matter).
    But it's fine to assign it to a proper pointer and use, as well:
    Code:
    char string[] = "My string";
    char* pStr = string;
    printf("%x", pStr);
    And btw, no & before any sort of array or you'll get type**, which isn't what you want.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    because "address" has a storage for it - called pointer
    if you want some "general" storage - use void* pointer - it will be able to receive the value of any pointer without cast

    int is a different type, that can even have different size

    you do not expect to convert double to int without cast? this is the same story
    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

  10. #10
    Registered User
    Join Date
    Feb 2008
    Posts
    30
    I don't get it. So only pointers can hold addresses right? But this assigns the address of string to the x variable:

    Code:
    int x = (int) string;
    So why does it work this way?

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    No, all types can hold addresses. But pointers is a special type that was designed to hold an address to something.
    Your way works because an address is just a number anyway, so any integer variable can be able to hold it.
    But the language defines that only pointers holds addresses, otherwise you need to explicitly tell the compiler you want a non-pointer to hold the address.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by daYz View Post
    I don't get it. So only pointers can hold addresses right? But this assigns the address of string to the x variable:

    Code:
    int x = (int) string;
    So why does it work this way?
    Happenstance.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  13. #13
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Code:
    #include <stdio.h>
    #include <stdint.h>
    #include <inttypes.h>
    
    int main()
    {
    	int i;
    	void *p = &i;
    	intptr_t ip = (intptr_t)p;
    
    	printf ("%" PRIxPTR "\n", ip);
    
    	return 0;
    }

  14. #14
    Registered User
    Join Date
    Feb 2008
    Posts
    30
    I need to go to bed. I'll continue with this tommorow. Thanks for your help people.

  15. #15
    Registered User
    Join Date
    Feb 2008
    Posts
    30
    Quote Originally Posted by Elysia View Post
    Your way works because an address is just a number anyway, so any integer variable can be able to hold it.
    That is why I still don't understand why this would not work:

    Code:
    int x = string;
    To me it looks like the address of string can be hold as a number by the integer variable.

    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM
  2. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM
  3. Im so lost at . .
    By hermit in forum C Programming
    Replies: 18
    Last Post: 05-15-2002, 01:26 AM
  4. pointerz
    By xlordt in forum C Programming
    Replies: 6
    Last Post: 01-11-2002, 08:31 PM
  5. Variable Allocation in a simple operating system
    By awkeller in forum C Programming
    Replies: 1
    Last Post: 12-08-2001, 02:26 PM