get value from an address

Jan 5, 2010 at 5:46am
Hi all..

I have a string containing an address:

string str = "0x0CA8";

assume 0x0CA8 is the address of some value.
my question is, how to get the value of address 0x0CA8 ?

Thanks.
Jan 5, 2010 at 5:50am
you need to use pointers but i don't have a clue how will you know its data type
Jan 5, 2010 at 5:55am
well void pointers dont know the data type but i dont think its good practice. Also i dont know if you can just goto an address like that. I dont even know how you would get that address of a variable into a string. like this

string str = &somestring; // i dont think this is correct at all

What exactly are you trying to do p4ilul. Are you trying to get a value from a pointer by dereferencing maybe?? We need to know in order to help but i dont think the above is possible or usefeul really
Jan 5, 2010 at 6:01am
hi blackcoder41,

that's the first thing crossed in my mind, use pointer. But how to assign the pointer to 0x0CA8 via str?

i use Qt's QVariant to store the value. QVariant is able to store various data type.
Jan 5, 2010 at 6:16am
btripp,

yes something like that.. dereferencing. But i don't have any idea to do it.

actually it comes from a device which gives a response like this:

"01 03 08 0C A8 41 E1..."

i store it in a string variable, parse it to get the 0CA8 part, then get the value of 0CA8.

i'm sorry if my question isn't clear enough.


Jan 5, 2010 at 6:27am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;

int main(int argc, char* argv[]) {
    string str = "0x0CA8";
    long int num = strtol (str.c_str(),NULL,16);//convert hex string to numeric
    cout << num << '\n'; //prove it
    char* p1 = 0;
    p1 = (char*)num; //cast num to pointer (note this might not work)
    cout << *p1 << '\n'; //note dereferencing might not work

    return 0;
}
Jan 5, 2010 at 6:53am
didn't work, blackcoder..
Jan 5, 2010 at 7:00am
I see what your saying I just dont know how to go about that though
Jan 5, 2010 at 7:03am
it is expected not to work, LOL
Jan 5, 2010 at 7:16am
blackcoder hit the nail on the head as far as I can tell.

1
2
3
volatile char* ptr = volatile char*(0x0CA8);

cout << int(*ptr);  // read the number at that address 


The thing that's really the problem is.... what makes you think that 0x0CA8 is a valid address? Raw I/O like this isn't wise unless you're doing really low-level hardware I/O (like writing a driver or something).
Jan 10, 2010 at 5:12am
actually i'm developing a Modbus RTU application. Do you have any recommendation what Windows open source Modbus library for me?

sorry for my late response. Thanks
Topic archived. No new replies allowed.