Does call by reference actually exist in c

pratap singh, upendra

pratap singh, upendra

@pratap-singh-6xlmve Oct 22, 2024
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...

Replies

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform

  • Anoop Kumar

    Anoop Kumar

    @anoop-kumar-GDGRCn Dec 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-#:
  • rahul69

    rahul69

    @rahul69-97fAOs Dec 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😀
  • Vishal Sharma

    Vishal Sharma

    @vishal-pysGmK Dec 2, 2012

    actually, while passing array you don't even need to write '&'
  • Ankita Katdare

    Ankita Katdare

    @abrakadabra Dec 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?