Storing the Address of a char???

This is a discussion on Storing the Address of a char??? within the C Programming forums, part of the General Programming Boards category; My question is fairly simple... i have an array of chars from an input file and the user needs to ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    18

    Storing the Address of a char???

    My question is fairly simple... i have an array of chars from an input file and the user needs to be able to input a memory location (im using an int) and see if anything in the array is located at said locations... so I need to know how to store the memory address of a char as an int, so i can compare it with the users inputed int...

    char a = 'z';
    char *ptr;
    ptr = &a;

    int user_address, real_address;
    scanf("%d", &user_address);

    printf("address of a: %d or %d\n", &a, ptr); - both of these print the address im looking for, but i cannot seem to figure out how to store that address so i can use it in an if statement.
    if(real_address == user_address) - need this to work

    real_address = (int)&a;
    real_address = (int)ptr;

    i would have thought one of those would work... but they won't compile. any ideas?

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Ok so are you trying to do this?

    Example:
    Code:
    char a = '.';
    int a_ptr = (int)&a;

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    18
    i think so. i just need an int (or double or whatever) that will hold the address of that char so i can compare it. when i do what you have there it gives me an error compiling... something about the size of the variables due to the cast

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    64-bit system? Why not just use casting to do direct comparisons? Post your exact code as you have it now, and I can post the corrections.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,058
    Addresses are unsigned quantities so cast the value of the pointer to an unsigned int or long.
    Code:
    unsigned real_address;
    real_address = (unsigned) &a;

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    18
    i do have a 64 bit computer, dual boot vista 64, and my linux is ubuntu, don't know how it handles that actually. My code is as simple as this....

    Code:
    int user_add, real_add;
    char a = 'y';
    scanf("%d", &user_add);     ---> user will enter 60123948 or something of this nature.
    real_add = ____________;
    if(user_add == real_add)
    {
    
    }
    I just need that _____ filled in so that real_add will hold the base 10 address of my char a.

  7. #7
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Ah right. It could have been a range error. Though it technically doesn't matter if you are going to be consistent all the way across the board.

  8. #8
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Why not just ditch the superfluous variables altogether?

    Code:
    unsigned long user_add;
    char a = 'y';
    scanf("%lu", &user_add);     ---> user will enter 60123948 or something of this nature.
    if(user_add == (unsigned long)&a)
    {
    
    }
    Or you could do this, which is kind of a nifty trick.

    Code:
    void *user_add;
    char a = 'y';
    scanf("%lu", &user_add);
    if(user_add == &a)
    {
    
    }

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Of course on a 64-bit system, you would need a bigger value than "int" to hold the value of a pointer. In Windows, there's a type defined in windows.h called "long_ptr" (or "ulong_ptr"), which is a integer value "big enough for a pointer on the system you are compiling for". You could do the same in Linux by using
    Code:
    typedef long long_ptr;
    In Linux, long is the correct size for a pointer in nearly all 32/64-bit versions, so using long should be fine.

    In windows, "long" is still 32-bit, so you need a different type to make it 64-bit.

    --
    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.

  10. #10
    Registered User
    Join Date
    Oct 2008
    Posts
    18
    ohhhh believe me i tried to skip the superfluousness, couldn't get it to work, so I figured I would make the question as easy to read as possible. I think you might be on to something with the unsigned long, i shall try this and let you know.

    thanks all

  11. #11
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    You can use a long unsigned long for windows. Actually you should change your scanf() argument to "%llu" for a 64-bit input.

  12. #12
    Registered User
    Join Date
    Oct 2008
    Posts
    18
    Thanks again that worked out, just to show you how bogus the int made the address when you stored it (printing worked for some reason)...
    Code:
    #include <stdio.h>
    int main()
    {
    	char a = 'y';
    	unsigned long add,user_add;
    	printf("Enter an address:\n");
    	scanf("&#37;llu", &user_add);
    	add = (unsigned long)&a;
    	printf("a = c:%c\n\n", a);
    	printf("&a = d:%d   lu:%lu  llu:%llu\n", &a);
    	printf("&user_add = d:%d   lu:%lu  llu:%llu\n",user_add );
    }
    OUTPUT:

    Enter an address:
    12345
    a = c:y

    &a = d:-776806577 lu:140191114246560 llu:140191114241920
    &user_add = d:12345 lu:140191114246560 llu:140191114241920

    So i don't know what the difference is between using llu and lu, but they both seem to work.

  13. #13
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    On your system (which is 64-bit) there shouldn't be a difference. Though you are causing a stack corruption with your call to printf() as it stands. I think the &#37;lu thing would be most consistent across platforms since a long on both of our systems would suffice to hold a pointer, even though you are 64-bit and I am 32-bit.

  14. #14
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Example:
    Code:
    #include <stdio.h>
    int main()
    {
    	char a = 'y';
    	unsigned long add,user_add;
    	printf("Enter an address:\n");
    	scanf("%llu", &user_add);
    	add = (unsigned long)&a;
    	printf("a = c:%c\n\n", a);
    	printf("&a = d:%p\n", &a);
    	printf("&user_add = d:%p\n",user_add );
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What does this do (Windows API)?
    By EVOEx in forum Windows Programming
    Replies: 4
    Last Post: 12-19-2008, 10:48 AM
  2. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 07:18 AM
  3. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 09:54 PM
  4. errors in class(urgent )
    By ayesha in forum C++ Programming
    Replies: 1
    Last Post: 11-10-2001, 10:14 PM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21