Pointers :-)

This is a discussion on Pointers :-) within the C Programming forums, part of the General Programming Boards category; Hi all! I have a text file where i saved the memory address as long integer. Now i have a ...

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    9

    Arrow Pointers :-)

    Hi all!

    I have a text file where i saved the memory address as long integer.

    Now i have a second routine which should read this memory address ---> works!

    The Problem:

    the variable int address includes the memory address but if i write this...

    int *pointer2address = &address;

    then the pointer2address pointer points to the memory addess of the variable address but not to the memory address which is included by the variable.

    Do you understand?

    Is their a way to to init the

    pointer2address and give it like this the memory position?

    pointer2address = &(5373507); ???

    Very horrible

  2. #2
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    That's not how you read text files. In order to read a text file you use a file pointer.

    //initialized to point to nowhere
    FILE *fptr = NULL;

    Than you open the file

    fptr = fopen("filename","r"); //open the file in read mode

    Than you read the file with fgets:

    fgets(...);

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    9
    Thatīs clear ... you missunderstood me.


    The Problem ist that i want directly give the pointer the memoryadress.

    for example : pointer = &memory_address;


    but how does this work?

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Why do you want to restore pointer address from a file in this manner? What are they pointing to?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    It's too dangerous to do that, however whatever you are trying to break into, remember that you can use an offset to your current pointer address. Do the math.

  6. #6
    Registered User
    Join Date
    Jul 2002
    Posts
    9
    The text file includes the memory addresses of Sprites attributes.

    I want to write a complex game editor that isn't limited.



    If nobody could help i have a new idea. --> Array of Sprite elements like a index structure.

    cu, fl_cody

    Programmer of Apidya 2002 http://apidya.vg-network.com

  7. #7
    Registered User
    Join Date
    Jul 2002
    Posts
    9
    Normally their should be no error with my pointers (but all programmers are saying that, or not )

    The addresses i am saving are including the offset already?!

  8. #8
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    http://www.fastgraph.com/qfsprite.html

    Maybe this link will help. It seems that your question is outside the scope of the C board. Instead of understanding how some standard C language capability works, you are asking a question related to game programming. Maybe the Windows or Game board will provide more help.

  9. #9
    Registered User
    Join Date
    Feb 2002
    Posts
    51
    If address holds a memory address already, then you don't need the '&' operator in front of it.

    int *pointer2address = address; //should work

    You need to be sure address points to the location of an int also. I think that's what you were asking about, then again I just woke up - so sorry if that doesn't help
    Last edited by drharv; 07-17-2002 at 08:49 AM.

  10. #10
    Registered User
    Join Date
    Jul 2002
    Posts
    9
    The question is not game related but ok... i will try the link.

    Thanks for your help!

  11. #11
    Registered User
    Join Date
    Jul 2002
    Posts
    9
    If address holds a memory address already, then you don't need the '&' operator in front of it.

    int *ptr = address;

    I think that's what you were asking about, then again I just woke up - so sorry if that doesn't help
    Hm? Donīt you think that the pointer ptr will get the value of the variable address?! It does !

    It doesnīt get the memory address in your example!

  12. #12
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Remember though that memory addresses are these huge long things, are you sure an integer will hold them? I don't think so, also this is not a correct practice, it is unsafe.

  13. #13
    Registered User
    Join Date
    Feb 2002
    Posts
    51

    Re: Pointers :-)

    Originally posted by fl_cody

    the variable int address includes the memory address but if i write this...

    int *pointer2address = &address ;

    then the pointer2address pointer points to the memory addess of the variable address but not to the memory address which is included by the variable.
    You said that the variable address contained the address that you wanted. If this is true, then just set pointer2address equal to it without the '&' operator. The '&' is used when you want the address of a current variable, not an address that is already stored.

  14. #14
    Registered User
    Join Date
    Feb 2002
    Posts
    51
    Originally posted by fl_cody


    Hm? Donīt you think that the pointer ptr will get the value of the variable address ?! It does !

    and yes, i think it would. You said that address held the address you wanted.

  15. #15
    Registered User
    Join Date
    Jul 2002
    Posts
    9
    Remember though that memory addresses are these huge long things, are you sure an integer will hold them? I don't think so, also this is not a correct practice, it is unsafe.
    Your are right but i used a long integer in my code.


    and yes, i think it would. You said that address held the address you wanted.
    Ok, i will test it and proof it :-)


    Thanks to all. Itīs a interesting thing but i have solved my main problem through an Sprite Index Array and so itīs not anymore so important.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. MergeSort with array of pointers
    By lionheart in forum C Programming
    Replies: 18
    Last Post: 08-01-2008, 10:23 AM
  2. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 12:18 PM

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