Thread: pointers and functions

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    630

    pointers and functions

    Hello..

    I have some program's source that I do not fully understand.
    Could someone please help me to explain/understand how pointers/arguments are passed betwen the functions:


    Code:
    class someclass {
    public:
    	char member;
    };
    
    void someotherfunc(ULONG_PTR *ptr) {
    	//here it calls windows api GetQueuedCompletionStatus with ptr and some other arguments (which will set ptr)
    }
    void somefunction(someclass *&sc) {  //why *& ?
    	someotherfunc((ULONG_PTR)&sc);
    }
    
    int main() {
    	someclass *sc_p = 0;
    	somefunction(sc_p);
    	return 0;
    }
    Thanks for help

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Guess your real problem is this
    Code:
    someclass *sc_p = 0;
    Many of the Windows API functions require a pointer to some data as parameter where they store some result. If you pass 0 or an uninitialized pointer they fail or crash.
    Guess you are talking about the lpCompletionKey parameter of GetQueuedCompletionStatus.
    you have to do something like this

    Code:
    void somefunction(ULONG_PTR * compl ) {
    	GetQueuedCompletionStatus( .., ...,  compl, ..., ... );
    }
    
    
    unsigned long compl;
    somefunction(& compl );  // pass a pointer to compl where GetQueuedCompletionStatus will store the CompletionKey
    Anyway you cannot pass a Null pointer to some class to this kind of functions.
    Kurt

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    someclass *&sc

    This declares a reference to a pointer to someclass. In other words, it passes a pointer, but the function can modify the pointer. It's like this:
    Code:
    void foo(int &i) // & makes it a reference
    {
      i = 4;
    }
    
    int main()
    {
        int bar = 3;
        foo(bar);
        std::cout << bar << std::endl; // Prints 4
    }
    Except that it's a reference to a pointer instead of an int.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    630
    Thanks for help guys.

    Kurt thats exactly what I am talking about.

    Quote Originally Posted by ZuK
    Code:
    void somefunction(ULONG_PTR * compl ) {
    	GetQueuedCompletionStatus( .., ...,  compl, ..., ... );
    }
    
    unsigned long compl;
    somefunction(& compl );  // pass a pointer to compl where GetQueuedCompletionStatus will store the CompletionKey
    Since when its unsigned long compl a pointer?
    Is it better to use ULONG_PTR instead of unsigned long?


    So can I do this instead:

    Code:
    void somefunction(ULONG_PTR *&compl ) {
    	GetQueuedCompletionStatus( .., ...,  compl, ..., ... );
    }
    
    unsigned long compl;
    somefunction(compl );
    Thank you

  5. #5
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by l2u
    Since when its unsigned long compl a pointer?
    it isn't.
    Quote Originally Posted by l2u
    Is it better to use ULONG_PTR instead of unsigned long?
    That WIN API function won't allocate any space for you. You have to pass a pointer to a variable that you have allocated.

    you could do sth like this but that doesn't make much sense.

    Code:
    unsigned long compl;
    ULONG_PTR compl_ptr = & compl;
    somefunction( compl_ptr );
    EDIT: I wonder why you want to pass a reference to a pointer. GetQueuedCompletionStatus won't change the pointer it will only dereference that pointer that you pass and store some result at this location. That WIN API functions are simple C API's, they don't understand references. If WIN API-functions want references to pointers then the parameters are pointers to pointers.
    Kurt
    Last edited by ZuK; 12-26-2006 at 05:19 PM.

  6. #6
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476
    Quote Originally Posted by CornedBee
    someclass *&sc

    This declares a reference to a pointer to someclass. In other words, it passes a pointer, but the function can modify the pointer. It's like this:
    Code:
    void foo(int &i) // & makes it a reference
    {
      i = 4;
    }
    
    int main()
    {
        int bar = 3;
        foo(bar);
        std::cout << bar << std::endl; // Prints 4
    }
    Except that it's a reference to a pointer instead of an int.
    What's the difference of reference of pointer and pointer of reference?
    ERROR: Brain not found. Please insert a new brain!

    “Do nothing which is of no use.” - Miyamoto Musashi.

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    A pointer to a reference cannot exist.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    Registered User
    Join Date
    Dec 2006
    Posts
    17
    Quote Originally Posted by l2u
    Since when its unsigned long compl a pointer?
    Is it better to use ULONG_PTR instead of unsigned long?
    You posted this code :
    Code:
    	someclass *sc_p = 0;
    	somefunction(sc_p);
    The * makes sc_p a pointer to "someclass" types. When you define a pointer, no memory is automatically allocated for the variable it points to (because you might want it to point to something which already exists!)

    You're probably looking to declare something on the stack and then pass a reference to it... In which case you need to do :
    Code:
    	someclass sc_p;
    	somefunction(&sc_p);
    ... and just declare somefunction without the &.

  9. #9
    Registered User
    Join Date
    May 2006
    Posts
    630
    Is this the same:
    If not, whats the difference?

    Code:
    void somefunc(int *c) {
    	*c = 2;
    }
    void somefunc2(int &c) {
    	c = 3;
    }
    
    int main() {
    	int c = 5;
    	somefunc(&c);
    	somefunc2(c);
    	return 0;
    }

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    It has the same effect.

    The main difference is that it's easier to use the first version incorrectly. On the other hand, some people (including Bjarne Stroustrup) prefer the pointer for parameters that get changed because the additional & at the function call site makes it clear that the value might be changed, without having to look at the parameter types of the function.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #11
    Registered User
    Join Date
    Dec 2006
    Posts
    17
    Quote Originally Posted by CornedBee
    some people (including Bjarne Stroustrup) prefer the pointer for parameters that get changed because the additional & at the function call site makes it clear that the value might be changed, without having to look at the parameter types of the function.
    Totally.

  12. #12
    Registered User
    Join Date
    Dec 2006
    Posts
    17
    Oh and for reference parameters that don't get changed... const all the way!!

  13. #13
    Registered User
    Join Date
    May 2006
    Posts
    630
    What about:

    Code:
    void somefunc3(int *&c) {
    	c = 4;
    }

  14. #14
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    That won't compile.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Conversion of pointers to functions
    By hzmonte in forum C Programming
    Replies: 0
    Last Post: 01-20-2009, 01:56 AM
  2. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  3. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  4. API "Clean Up" Functions & delete Pointers :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 05-10-2002, 06:53 PM
  5. pointers, functions, parameters
    By sballew in forum C Programming
    Replies: 3
    Last Post: 11-11-2001, 10:33 PM