Thread: functions and pointers (call by value)

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    144

    functions and pointers (call by value)

    with the below code what exactly below?

    Is the address of 'a' being copied into the address of 'i'? If so, when I state temp=*i; does that mean that the address of a is copied into temp. *i means contents of i which is '&a'
    Code:
    #include <stdio.h>
    
    int fun1(int *i,int *j)
    
    {
    int temp;
    temp=*i;
    *i=*j;
    *j=temp;
    return(0);
    }
    
    int main()
    
    {
    	int a,b;
    	scanf_s("%d%d",&a,&b);
    	printf("Before call %d %d \n",a,b);
    	fun1(&a,&b);
    	printf("After first call %d %d \n",a,b);
    	getchar();
    	getchar();
    return(0);
    }
    Last edited by bos1234; 02-14-2011 at 06:36 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by bos1234
    Is the address of a being copied into the address of i?
    No. Think about the type of i.

    Quote Originally Posted by bos1234
    If so, when address of a and address of b are swapped should it not change in the main function
    Print these addresses as a test.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    printf("Hello Cboard\n"); codeprada's Avatar
    Join Date
    Jan 2011
    Location
    In a little room at the back of your brain
    Posts
    68
    *i is a pointer so technically it's taking the address of a so basically yes the address of a is in i. this means that *i will point to the variable located in the address assigned to it which is a in this case

    if you swap i and j's address they won't affect a and b. this will only make the pointers point to different variables in memory. but i see you are actually swapping the values in fun1 by using *i = *j; to swap memory addresses use this

    Code:
    int fun1(int *i,int *j)  
    { 
        int * temp; 
        temp=i; 
        i=j; 
        j=temp; 
        return(0); 
    }
    We shouldn't be quick to criticize unless we can do better.
    Code:
    public function __clone() { die ( "I'm one of a kind" ); }

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
    int fun1(int *i,int *j)    <--- i = address of a, j = address of b
    {
    int temp;      <--- integer value
    temp=*i;      <--- temp = value at address of i  
    *i=*j;           <---  value at i = value at j
    *j=temp;      <--- value at j = temp
    return(0);     <--- return 0
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array of functions
    By frktons in forum C Programming
    Replies: 29
    Last Post: 06-30-2010, 09:51 AM
  2. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 04:25 AM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  4. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  5. API "Clean Up" Functions & delete Pointers :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 05-10-2002, 06:53 PM