Pointer confusion

This is a discussion on Pointer confusion within the C++ Programming forums, part of the General Programming Boards category; I feel confusion about pointers which I would like to remove. Code: void SomeClass::ConvertAndForward( int numbers, int targetvar ) { ...

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    34

    Pointer confusion

    I feel confusion about pointers which I would like to remove.


    Code:
    void SomeClass::ConvertAndForward( int numbers, int targetvar )	
    {
    	//Send to member variable
    	targetvar = numbers;
    
    	
    
    
    }
    When I call this function with:
    ConvertAndForward(10, m_ivariable);

    Will the above method successfully change m_ivariable? As in, put it through the function, modify it and put in back in place with the new value?

    Or do I need a pointer version like so:


    Code:
    //called with ConvertAndForward(10, ptarget);
    void SomeClass::ConvertAndForward( int numbers, int *pointer_to_targetvar )	
    {
    	//Send to member variable
    	pointer_to_targetvar = numbers;//This produces an error
    
    //error C2440: '=' : cannot convert from 'int' to 'int *'
    
    }

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,763
    Will the above method successfully change m_ivariable? As in, put it through the function, modify it and put in back in place with the new value?
    If m_ivariable is being changed from within the class, you don't need to pass it around, as all class methods will have access. If a friend class needs to change the member variable, you should pass in an object instead.

    Or do I need a pointer version like so:
    Anyway, the first version in your post uses pass by value semantics, so the function will modify a copy of the argument and you would have to return the modified variable to see the effects. Your second version would work if you used references instead of pointers, and frankly I see no reason not to use references. Here is a fixed version anyway. Change the function that uses pointers like so:
    Code:
    *pointer_to_targetvar = numbers;

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,033
    Code:
    void SomeClass::ConvertAndForward( int numbers, int* pointer_to_targetvar )
    {
        //Send to member variable
        *pointer_to_targetvar = numbers; // Remember to dereference and not change the address
    }
    
    // Or...
    
    void SomeClass::ConvertAndForward( int numbers, int& pointer_to_targetvar )
    {
        //Send to member variable
        pointer_to_targetvar = numbers; // Works just as is
    }
    Last edited by Elysia; 07-28-2012 at 03:16 PM.
    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.

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,763
    *pointer_to_targetvar = numbers; // Remember to dereference and not change the address
    The comment is not correct; assigning an integer to a pointer is an error, and will not change the address.

    The reference version doesn't compile as of this writing, either.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,033
    Quote Originally Posted by whiteflags View Post
    The comment is not correct; assigning an integer to a pointer is an error, and will not change the address.
    You are right. I was considering the concept. You don't want to change the address, ie what it is pointing to, but want to actually change the value of what it is pointing to.

    The reference version doesn't compile as of this writing, either.
    I honestly have no idea how I made that syntax error... Anyway, fixed. Thanks for pointing out that.
    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.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Quote Originally Posted by Jefff View Post
    Will the above method successfully change m_ivariable?
    No, definitely not.

    Or do I need a pointer version like so:
    No, you don't.

    What you want is to just modify the member variable directly, without passing it in at all. I.e. Just do m_ivariable = numbers;
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer confusion
    By boxden in forum C++ Programming
    Replies: 2
    Last Post: 10-03-2010, 05:00 AM
  2. Pointer confusion
    By rits in forum C Programming
    Replies: 3
    Last Post: 03-12-2009, 09:00 AM
  3. Replies: 1
    Last Post: 03-24-2008, 11:16 AM
  4. Pointer confusion
    By mart_man00 in forum Linux Programming
    Replies: 3
    Last Post: 02-14-2003, 06:19 PM
  5. pointer confusion
    By badhri in forum C Programming
    Replies: 6
    Last Post: 05-07-2002, 07:08 AM

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