Thread: Using Pointers in a function

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    2

    Exclamation Using Pointers in a function

    I am learning C, and am am trying to create my own "swap" function for practice that takes in two numbers and puts them in two different variables. I then set one pointer to each. This is where I get stuck.

    What should I pass into the function parameters: by this I mean do I pass say, *num1, or the address it points to, num1. I am confused. Please provide code examples.


    Thanks

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Yes, the address.

    Code:
    void swap( int* a, int* b );
    
    int main( void )
    {
    	int 
    		i, 
    		j;
    	swap( &i, &j );
    	int
    		* p = &i, 
    		* q = &j;
    	swap( p, q );	
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by greeneyehawk View Post
    Please provide code examples.
    It will probably be easier if you provide code examples. But I think Sebastiani might have nailed this one.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User ITAmember's Avatar
    Join Date
    Mar 2009
    Location
    Nebraska
    Posts
    72
    I'm assuming you know how memory addresses work. I'm also assuming other people will post code for you. This is just how I learned how pointer operators work.

    * gets the value at an address
    & gets the address of a value

    That's it. They really aren't that complicated once you understand them

  5. #5
    Registered User
    Join Date
    Jun 2009
    Posts
    2

    Smile Thanks!

    Thank you so much, I got it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  2. Problem with function pointers
    By vNvNation in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2004, 06:49 AM
  3. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. function pointers and member functions
    By thor in forum C++ Programming
    Replies: 5
    Last Post: 03-19-2002, 04:22 PM

Tags for this Thread