Thread: Store some addresses of variable to integer variable

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    116

    Question Store some addresses of variable to integer variable

    I want to save address of the pointer point to to my variable:
    So, I do that:
    Code:
     
    <Pointer>*a;
    int tmp=*a; //hope that tmp will store address that pointer a point to
    but the complier will give me an error:error: invalid conversion from 'Pointer**' to 'int.
    So, how can I do that, please help me, please.

    thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Why do you want to do this?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Aug 2011
    Posts
    116
    Ah, I have a complete binary tree building by linked List. and of course, go to some node more difficult than if use array. and I want to convert this tree to array. (mean array[1]=address of first node, array[2]=address of second node, array[3]=address of third node,....). after that, I just use:
    Code:
    <pointer>*tmp=&array[i];
    And, don't ask me, why I don't use array instead of linked list zz

    thanks
    Last edited by hqt; 09-04-2011 at 11:39 AM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by hqt
    Ah, I have a complete binary tree building by linked List. and of course, go to some node more difficult than if use array. and I want to convert this tree to array. (mean array[1]=address of first node, array[2]=address of second node, array[3]=address of third node,....).
    Create an array of pointers to the elements of the tree. Traverse over the tree and populate the array.

    Quote Originally Posted by hqt
    And, don't ask me, why I don't use array instead of linked list zz
    Why don't you use an array instead of a linked list?

    (Generally, I would not regard a tree as a linked list, though links are involved.)
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Sep 2009
    Posts
    48
    You want a pointer to a pointer?

    Code:
    int* p		= new int;
    int** pp	= &p;
    Then pp contains the address of the pointer p.

    You can even do this, though I'm not sure why you would:

    Code:
    int add		= (int)&p;

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It should be noted that
    int add = (int)&p;
    may result in truncation since the size of an int is not necessarily the size of a pointer (64-bit windows is a prime example).
    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.

  7. #7
    Registered User
    Join Date
    Aug 2011
    Posts
    116
    Ah, array of pointer, I almost forget it :">
    And yes, of course when building tree, array is more suitable than linked list in most case. But linked list is "natural", and some applications may be use linked list is absolutely compulsory (example fibbonaci Heap)

    thanks
    Last edited by hqt; 09-04-2011 at 09:23 PM.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What I meant was that the concept of a tree and the concept of a linked list are distinct. A tree generally would have some kind of branching (even if it effectively just a linked list in special cases) whereas a linked list is a linear sequence. That you could use an array to implement some kinds of tree structures and use links to nodes to implement other tree structures is a separate matter.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Store pointee to an int variable
    By limp in forum C Programming
    Replies: 7
    Last Post: 05-25-2011, 03:17 PM
  2. Extract values and store in new variable
    By cosmiccomputing in forum C Programming
    Replies: 3
    Last Post: 06-01-2008, 01:45 PM
  3. which variable can store words?
    By Hunterofman in forum C++ Programming
    Replies: 8
    Last Post: 04-28-2008, 05:59 PM
  4. variable addresses and pointers
    By tzpb8 in forum C Programming
    Replies: 47
    Last Post: 07-26-2006, 07:08 PM
  5. store date in a variable
    By actionbasti in forum C++ Programming
    Replies: 2
    Last Post: 10-01-2003, 09:50 PM