C does not directly support pass by reference

Thread Starter

Dadu@

Joined Feb 4, 2022
155
Hello

When I read page in link then I get confused because it is written in it that C does not pass by reference

https://clc-wiki.net/wiki/C_language:Terms:Pass_by_reference

I think when we want to pass parameter to function we can pass the parameter by reference or by value.

Pass by value means, the copy of the variable value is passed to the function.
int X = 1;

To pass the parameter by reference we specify &X

X
is passed by reference, i.e. by its address.
 

BobTPH

Joined Jun 5, 2013
11,515
That is passing a pointer by value. You have to use & to pass it and * to refer to it in the function.

Compare to pascal:

procedure by_value(x : integer)
procedure by_reference(var x : integer)

by_value(x);
by_reference(x);

You do not need any & or *

Bob


Bo
 

nsaspook

Joined Aug 27, 2009
16,321
https://dept-info.labri.fr/~strandh...ndh-Tutorial/uniform-reference-semantics.html

A very useful, powerful and potentially dangerous common programming practice.

Pass one data reference to create pointers in a thread to several data types.

C:
static int32_t daqgert_ao_thread_function(void *data)
{
    struct comedi_device *dev = (void*) data;
    struct comedi_subdevice *s = &dev->subdevices[2];
    struct daqgert_private *devpriv = dev->private;
    struct spi_param_type *spi_data = s->private;
    struct spi_device *spi = spi_data->spi;
    struct comedi_spigert *pdata = spi->dev.platform_data;

    if (!dev)
        return -EFAULT;
    dev_info(dev->class_dev, "ao device thread start\n");

    while (!kthread_should_stop()) {
        if (likely(test_bit(AO_CMD_RUNNING, &devpriv->state_bits))) {
            daqgert_handle_ao_eoc(dev, s);
            pdata->kmin = ktime_set(0, pdata->delay_nsecs);
            __set_current_state(TASK_UNINTERRUPTIBLE);
            schedule_hrtimeout_range(&pdata->kmin, 0,
                HRTIMER_MODE_REL_PINNED);
        } else {
            clear_bit(SPI_AO_RUN, &devpriv->state_bits);
            smp_mb__after_atomic();
            wait_event_interruptible(daqgert_ao_thread_wq, test_bit(AO_CMD_RUNNING, &devpriv->state_bits));
            smp_mb__before_atomic();
            set_bit(SPI_AO_RUN, &devpriv->state_bits);
            smp_mb__after_atomic();
        }
    }

    return 0;
}
 
Last edited:

Thread Starter

Dadu@

Joined Feb 4, 2022
155
That is passing a pointer by value. You have to use & to pass it and * to refer to it in the function.
I'm not sure but i think I am passing sturucture by its value in following code. Can you please confirm ?

C:
#include<stdio.h>
#include<stdlib.h>

struct s
{
    int x;
};

void foo ( struct s  var)
{
  var.x = 1;
 
}
int main()
{
   struct s var;
  
   var.x = 0;
 
   printf(" before calling,  var = %d \n", var);
  
   foo ( var );
    
   printf(" after calling ,  var = %d \n", var);
 
  
   return 0;   
}
before calling, var = 0
after calling , var = 0
 

Thread Starter

Dadu@

Joined Feb 4, 2022
155
@BobTPH

Can you also please confirm if I am passing structure by its refrence ?

C:
#include<stdio.h>
#include<stdlib.h>

struct s
{
    int x;
};

void foo ( struct s * var)
{
  var -> x = 1;

}
int main()
{
   struct s var;
 
   var.x = 0;

   printf(" before calling, var = %d \n", var);
 
   foo ( &var );
   
   printf(" after calling, var = %d \n", var);

 
   return 0;  
}
before calling, var = 0
after calling, var = 1
 

click_here

Joined Sep 22, 2020
548
Depending on who you talk to, C does not have pass by reference. The reason is that in C pointers are just another variable (unlike VB.NET, java, ect...)

Taking VB.NET where you don't have pointers (and you don't want to pass a large object to a function), you would use a pass by reference. (ByVal vs ByRef)
Code:
Sub Calculate(ByVal rate As Double, ByRef debt As Double)
    debt = debt + (debt * rate / 100)
End Sub
But that's not a problem in C, because we actually have pointers as variables, we can change it's value (i.e. incrementing for a home-made 'puts()' function)

Can you also please confirm if I am passing structure by its refrence ?
What you are doing is the same as passing by ref, but in C you would just say "a pointer to".
 
Top