pointer to known address

I want to set a pointer to point to a known address. eg. an address that comes in to the program as an argument and I want to read the contents on that address. See code-example below.


/M


main()

{

int *p, *p2;

int tal=8, i;

char c[10];


p=&tal;


cout << p << endl;<p>
sprintf(c, "%i", p);


i = atoi(c);


//What to do??????????

//I want to set p2 to point at the same address as p without doing p=p2;




}


Comments

  • : I want to set a pointer to point to a known address. eg. an address that comes in to the program as an argument and I want to read the contents on that address. See code-example below.


    : /M


    : main()

    : {

    : int *p, *p2;

    : int tal=8, i;

    : char c[10];


    : p=&tal;


    : cout << p << endl;<p>
    : sprintf(c, "%i", p);


    : i = atoi(c);


    : //What to do??????????

    : //I want to set p2 to point at the same address as p without doing p=p2;


    :

    :


    p=p2 wouldn't set p2 to point to anything anyway. It would set p to point to whatever random address p2 was pointing to.


    You could do p2=p to set p2 to point to the same thing as p. You could also do p2=&tal just like you did with p. Either will accomplish the same thing.





  • I know I can do what you told me, but the problem is that I want to create a pointer in one program and start another program and send the address as an argument and in this program read what the other program puts at that address.


    Maybe it can be solved in some other way, not including pointers.

    The program that sends is a win 32 program and the reciever is ported to dos.


    /M

    : p=p2 wouldn't set p2 to point to anything anyway. It would set p to point to whatever random address p2 was pointing to.

    : You could do p2=p to set p2 to point to the same thing as p. You could also do p2=&tal just like you did with p. Either will accomplish the same thing.







  • : I know I can do what you told me, but the problem is that I want to create a pointer in one program and start another program and send the address as an argument and in this program read what the other program puts at that address.




    That's simple enough. When you start the new program you presumably pass it an argument list. That means you probably have the receiving program's main defined:


    int main(int argc, char **argv)


    or something of the sort. Convert the pointer into an unsigned long integer (typecast it), and use sprintf to dump it into a string as one of the string parameters passed to the receiving program. In the receiving program, use sscanf to pull out the unsigned long from the proper index in argv and typecast it back to the proper pointer type.


    Here's the ugly though. If you're starting up an arbitrary program to work in this manner, the new program will probably not have access to the first program's memory. Depending on the particular operating system you're using, the first program's memory will be protected from the point of view of the second program. Accessing it will give you a general protection fault.


    You'd need to find a way to create 'shared memory' in the first program and do your work in there. I'm sure there's Windows calls to deal with shared memory.





    : The program that sends is a win 32 program and the reciever is ported to dos.


    Then you'll almost certainly run into memory protection issues.





Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories