Thread: pass by address program

  1. #1
    Registered User
    Join Date
    Jul 2018
    Posts
    80

    pass by address program

    I need help in my program

    Code:
    #include <stdio.h> 
    void pass (int *x);
     
     
    int main(void)
    {
        int a = 12; 
        printf("address of a is : %p \n", &a);
        printf("value of a is : %d\n", a);  
         
        pass (&a);
         
        printf("after calling address of a is : %p\n", &a);
            printf("after calling value of a is : %d\n", a);    
         
        return 0;
    }
     
     
    void pass (int *x)
    {
        
       *x = 10;
        
       printf("address of x is : %p\n", x); 
       printf("value of x is : %d\n", *x);  
        
    }
    Program result
    address of a is : 0061FF2C
    value of a is : 12
    address of x is : 0061FF2C
    value of x is : 10
    after calling address of a is : 0061FF2C
    after calling value of a is : 10
    Last edited by vajra11; 11-29-2019 at 10:30 PM.

  2. #2
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,766
    The address has not been changed. In this code:

    Code:
        printf("address of a is : %p \n", a);
     // ...
    
    
    
        printf("after calling address of a is : %p\n", a);
    Is 'a' an address? No, &a is though. I think now is a good time to turn on warnings for your compiler.

  3. #3
    Registered User
    Join Date
    Jul 2018
    Posts
    80
    Quote Originally Posted by Hodor View Post
    The address has not been changed. In this code:
    you posted hint while I was editing post.

    pass by value

    Code:
    #include <stdio.h> 
    void pass (int x);
     
     
    int main(void)
    {
        int a = 12; 
        printf("address of a is : %p \n", a);
        printf("value of a is : %d\n", a);  
         
        pass (a);
         
        printf("after calling address of a is : %p\n", a);
            printf("after calling value of a is : %d\n", a);    
         
        return 0;
    }
     
     
    void pass (int x)
    {
        
       x = 10;
        
       printf("address of x is : %p\n", &x); 
       printf("value of x is : %d\n", x);  
        
    }
    address of a is : 0000000C
    value of a is : 12
    address of x is : 0061FF10
    value of x is : 10
    after calling address of a is : 0000000C
    after calling value of a is : 12

    if I compare both program pass by address code is more meaningful because it save the memory of hardware. correct me if I am wrong

  4. #4
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,766
    ok, in main() 'a' is an integer, not a pointer to an integer so change these lines (line 8 and 13)

    Code:
    printf("address of a is : %p \n", a); /* line 8 */
    printf("after calling address of a is : %p\n", a); /* line 13 */
    to

    Code:
    printf("address of a is : %p \n", &a);
    printf("after calling address of a is : %p\n", &a);
    Because you want a pointer to 'a', not 'a' itself (which is why I added the & before the a)

    Ok, now that's fixed, what compiler are you using because there is no modern compiler that I am aware of that will not warn that you're passing the wrong datatype (an int and not a pointer to int, in this case)?

  5. #5
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,766
    Quote Originally Posted by vajra11 View Post
    if I compare both program pass by address code is more meaningful because it save the memory of hardware. correct me if I am wrong
    No, that's wrong. You're passing the address because you want to modify the value of 'a' and for that modified value be reflected in the calling function (in this case, main()).

    I just noticed that your new program is wrong. The new version will not modify the value of x (which is a copy of a from main). C only has pass by value, so 'x' (in pass()) is a copy of 'a' (from main()) so the value of 'a' in main will remain unchanged because pass() made a copy and worked on the copy. If you want to change the value of 'a' in main then do this:

    Code:
    #include <stdio.h> 
    void pass (int x);
      
      
    int main(void)
    {
        int a = 12; 
        printf("address of a is : %p \n", &a);
    
        printf("value of a is : %d\n", a);  
    
          
    
        pass (&a);
    
          
    
        printf("after calling address of a is : %p\n", &a);
    
        printf("after calling value of a is : %d\n", a);    
    
          
        return 0;
    }
      
      
    void pass (int *x)
    
    {
         
       *x = 10;
    
         
       printf("address of x is : %p\n", x); 
    
       printf("value of x is : %d\n", *x);  
    
         
    }

  6. #6
    Registered User
    Join Date
    Jul 2018
    Posts
    80
    Quote Originally Posted by Hodor View Post

    I just noticed that your new program is wrong. The new version will not modify the value of x (which is a copy of a from main). C only has pass by value, so 'x' (in pass()) is a copy of 'a' (from main()) so the value of 'a' in main will remain unchanged because pass() made a copy and worked on the copy. If you want to change the value of 'a' in main then do this:
    Thank you Hodor for clear explanation

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 11-16-2015, 01:49 PM
  2. Replies: 1
    Last Post: 08-24-2015, 07:43 AM
  3. pass two string in this program...
    By king_zart in forum C++ Programming
    Replies: 11
    Last Post: 12-11-2012, 09:12 PM
  4. how to pass a file name to my program ?
    By GSalah in forum C++ Programming
    Replies: 4
    Last Post: 11-07-2006, 12:28 PM
  5. Should i pass address of pointer or just pointer???
    By howhy in forum C++ Programming
    Replies: 11
    Last Post: 09-02-2005, 04:05 AM

Tags for this Thread