Pointer's address don't change

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • doreply
    New Member
    • Aug 2007
    • 7

    Pointer's address don't change

    I've got code:

    [CODE=cpp]struct Cell{
    list<int> vertices;
    bool discrete;
    Cell* nextCell;
    }

    struct SearchNode{
    Cell* partition;
    SearchNode* nextSearchNode;
    }

    bool isDiscrete(Cell * c){
    while(c!=NULL){
    if(!c->discrete)
    return false;
    c = c->nextCell;
    }
    return true;
    }

    void stabilise(Searc hNode* s){
    .......
    Cell* c = s->partition;
    isDiscrete(c);
    }[/CODE]

    after isDiscrete(c) call, c address never change, it always point to head. Any idea?
    Last edited by Ganon11; Aug 28 '07, 02:32 AM. Reason: Please utilize the CODE tags when writing your post.
  • gsi
    New Member
    • Jul 2007
    • 51

    #2
    HI,

    after isDiscrete(c) call, c address never change, it always point to head.
    You are passing 'c' to the isDiscrete function as pass by value . So the value of c is actually copied over and used in the isDiscrete function. Although 'c' is a pointer, you can change the value in memory to which 'c' is pointing to , not the value of c itself.

    Use pass by reference instead.

    it is like,

    [code=cpp]
    void stabilise(Searc hNode* s){
    isDiscrete(c); // function call.
    }
    isDiscrete(Cell & * c){
    }
    [/code]

    Thanks,
    gsi.
    Last edited by Ganon11; Aug 28 '07, 02:32 AM. Reason: fixing code tags

    Comment

    • doreply
      New Member
      • Aug 2007
      • 7

      #3
      Originally posted by gsi
      HI,

      Although 'c' is a pointer, you can change the value in memory to which 'c' is pointing to , not the value of c itself.
      How can I kown it is passed by value or it is passd by address when the function has parameter as pointer?

      Comment

      • gsi
        New Member
        • Jul 2007
        • 51

        #4
        Hi,

        isDiscrete(Cell & * c){
        }
        Sorry, It must have been,

        [code=cpp]
        isDiscrete(Cell *& c){
        [/code]

        How can I kown it is passed by value or it is passd by address when the function has parameter as pointer
        The function parameter may be a pointer, pointer to a pointer ...etc , but the default parameter passing mechanism is Pass by value in (c++) unless you expilicitly do something like the one above to pass by reference or the one below (pass address by value),

        Code:
        bool isDiscrete(Cell** c){
        //
        }
        return true;
        }
        
        void stabilise(SearchNode* s){
        .......
        Cell* c = s->partition;
        isDiscrete(&c);
        }
        In pass by value, argument's value are copied over to the parameters,
        In pass address by value , argument's address are copied over to the parameters.
        In pass by reference(C++), parameters become aliases to the arguments (but internally still manipulated using pointers)
        Thanks,
        gsi

        Comment

        • doreply
          New Member
          • Aug 2007
          • 7

          #5
          Thanks! I understand it now. Doreply.

          Comment

          • doreply
            New Member
            • Aug 2007
            • 7

            #6
            The other question is:
            Code:
            extern Node* this_node;
            .....
            
            struct SearchNode{
              Cell* partition;
              Node* node;
              SearchNode(){
            	node=NULL;
            	partition=NULL;
            	nextSearchNode=NULL;
              }
            }
                  
            void partition(Cell* c){
                ....   // here i used this_node
            } 
                    
            void newFunction(SearchNode* s){  
                                     // SearchNode* s = new SearchNode; 
                                     // s->partition = ....; 
               Cell* c = s->partition;
               partition(c);
               ......
            }
            I checked the value after run the program:
            before partition(c): s->node = NULL, s->nextSearchNo de = NULL
            after: s->node is not NULL, s->nextSearchNo de still is NULL.

            Why s->node changed?

            Comment

            • gsi
              New Member
              • Jul 2007
              • 51

              #7
              Hi,
              struct SearchNode{
              Cell* partition;
              Node* node;
              SearchNode(){
              node=NULL;
              partition=NULL;
              nextSearchNode= NULL;
              }
              }
              I dont see nextSearchNode declared inside the structure.

              Thanks,
              gsi.

              Comment

              • doreply
                New Member
                • Aug 2007
                • 7

                #8
                Originally posted by gsi
                I dont see nextSearchNode declared inside the structure.
                That's why I feel strange. I used debugger and used hard code to check it, the debugger: before partition(c): s->node = 00 after s->node = 04. I've checked all the codes. The only suspect place affect the code is: extern Node* this_node which I used in partition function. When I enable this_node in partition function, s->node will change, otherwise, no problem at all. But this_node is irrelavent to SearchNode and SearchNode* s is not in partition function. Very strange!

                Comment

                Working...