CrazyEngineers
  • Does call by reference actually exist in c

    pratap singh, upendra

    pratap singh, upendra

    @pratap-singh-6xlmve
    Updated: Oct 22, 2024
    Views: 1.4K
    hi,

    when dealing with arrays we often come across mechanism that seems to implement call by reference, of course, during function calls.

    Is it not the violation of the fact that 'all function calls in c are by value only', or is there any mechanism other than call by reference and call by value that is being implemented?

    kindly explain...
    0
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • Anoop Kumar

    MemberDec 1, 2012

    When ever you pass a variable to function in arguments, it's pass by reference.
    When you use pointers its pass by reference, as pointer directly points to original address.
     Pass by value
     
    void foo(int j) {
      j = 0;  /*  modifies the copy of the argument received by the function  */
    }
     
    int main(void) {
      int k=10;
      foo(k);
      /*  k still equals 10  */
    }
     
    pass by refence.
     
    If you do want a function to modify its argument you can obtain the desired effect using pointer arguments instead:
     
         
     
    void foo(int *j) {
      *j = 0;
    }
     
    int main(void) {
      int k=10;
      foo(&k);
      /*  k now equals 0  */
    }
    
    #-Link-Snipped-#:
    Are you sure? This action cannot be undone.
    Cancel
  • rahul69

    MemberDec 1, 2012

    Arrays are always passed by reference, regardless of C or C++. In C++, pass by reference takes place by use of '&' and in C, the same reference is done using pointers😀
    Are you sure? This action cannot be undone.
    Cancel
  • Vishal Sharma

    MemberDec 2, 2012

    actually, while passing array you don't even need to write '&'
    Are you sure? This action cannot be undone.
    Cancel
  • Ankita Katdare

    AdministratorDec 2, 2012

    That is a good question. I want to know, where did you get
    violation of the fact that 'all function calls in c are by value only'
    that from? Is it mentioned in some book?
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register