Object Oriented Concepts and Programming

Blog is for Object oriented concepts and prgramming. Specially for MCA GTU (Gujarat Technological University) student

RSS
  • Home
  • Edit

What actually passing by address means internally, pass by address is passing as value

Saturday, May 22, 2010 by Vishal Joshipura | 0 comments
When you pass an address to a function, that address is actually passed by value!
Because the address is passed by value, if you change the value of that address
within the function, you are actually changing a temporary copy. Consequently,
the original pointer address will not be changed!

Example:

#include "iostream"
#include "conio.h"
using namespace std;

void fun_test(int *);
int main()
{
    int value=50;  
    int *ptr = &value;      // Which means *ptr = 50
   cout <<<"Value Before : "<< *ptr;      // This will print 50
    fun_test(ptr);
    cout <<<"Value after : "<< *ptr;         // This will print 50
    getch();
    return 0;
}

void fun_test(int *arg_ptr)
{
    int fun_value=10;
    arg_ptr = &fun_value;    // This will print 10
    cout <<<"Value in function : "<< *arg_ptr;
}


Question :
         We are pass argument by address and if it is pass as value
         then why the change in its value is affected at the original
         calling place?

Answer   :
         When you change the value at the address
         then it will affect to the original memory address's
         content so its effect will still remains at the place
         from where the function calls and argument's
         address will be change by function statement.

Argument, Pass by address, Pass By argument

OOCP Material

Wednesday, May 5, 2010 by Dr. Parag Shukla | 0 comments


Download

OOCP Material

Subscribe to: Comments (Atom)

Blog Archive

  • ▼  2010 (10)
    • ►  November (1)
    • ►  August (3)
    • ▼  May (2)
      • What actually passing by address means internally,...
      • OOCP Material
    • ►  April (3)
    • ►  March (1)

Followers

Labels

  • Argument (1)
  • Class (1)
  • Defualt Argument (1)
  • Fact (1)
  • Factorial (1)
  • Item (1)
  • OOCP Material (1)
  • Pass by address (1)
  • Pass By argument (1)
  • Recursive Function (1)

Contributors

  • Dr. Parag Shukla
  • Vishal Joshipura
  • vaibhav

Pages

  • Home
Copyright © 2010 Object Oriented Concepts and Programming