Thread: Passing pointers

  1. #1
    Bond sunnypalsingh's Avatar
    Join Date
    Oct 2005
    Posts
    162

    Passing pointers

    Why in C++ passing a pointer is not termed as a parameter passing technique.....Is it because it has a flaw of some kind i.e. address is not reflected back/we can change the address in called function..is that so...if that's the reason....then we can pass it as const pointer as a remedy.....

    Plz comment

  2. #2
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    I'm confused as to what you're asking. Post some code and/or clarify your question.

    Passing pointers to (I assume) functions is valid C++. Take the following:
    Code:
    #include <iostream>
    
    void inctwice(int *y)
    {
    	*y += 2;
    }
    
    int main()
    {
    	int x = 2;
    
    	inctwice(&x);
    	std::cout << "Value of x: " << x << std::endl;
    }
    That will say that x is four at the end of the program. The same could be accomplished with references as well.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  3. #3
    Bond sunnypalsingh's Avatar
    Join Date
    Oct 2005
    Posts
    162
    What i mean is why is it not considered as a parameter passing technique in C++

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    It is considered as a parameter passing technique in C++.

    References are often preferred because you don't need to check for null (since pointers can legally be null), and because the syntax of a reference is more natural.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Maybe for most purposes references provide a safer and cleaner way of achieving the same effect as pointers.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing a string array to a function using pointers
    By asofaihp in forum C++ Programming
    Replies: 2
    Last Post: 04-13-2009, 11:31 AM
  2. Passing Pointers by reference
    By Bladactania in forum C Programming
    Replies: 10
    Last Post: 02-13-2009, 10:14 AM
  3. passing pointers outside of functions
    By thomas41546 in forum C Programming
    Replies: 6
    Last Post: 01-26-2008, 06:00 PM
  4. Passing pointers
    By cyberCLoWn in forum C++ Programming
    Replies: 6
    Last Post: 04-14-2004, 12:17 PM
  5. Pointers and reference passing
    By Denis Itchy in forum C++ Programming
    Replies: 4
    Last Post: 12-13-2002, 01:36 AM