How to make a pointer by reference in C

1

Well what I said in C ++ compiles this code for me without problems:

    void pasoPorReferencia(int *&referencia){

      cout<<referencia<<endl;

      int *dir_cambio, m = 2;
      dir_cambio = &m;

      cout<<dir_cambio<<endl;

      referencia = dir_cambio;

      cout<<referencia<<endl;

    }

    int main(int argc, char const *argv[]) {

      int *dir_entero, n = 13;

      dir_entero = &n;

      cout<<dir_entero;
      pasoPorReferencia(dir_entero)<<endl;

      cout<<dir_entero<<endl;

      return 0;
    }

And in C:

    void pasoPorReferencia(int *&referencia){

      printf(" %p \n",referencia);

      int *dir_cambio, m = 2;
      dir_cambio = &m;

      printf(" %p\n",dir_cambio);

      referencia = dir_cambio;

      printf(" %p",referencia);
    }

Launches the following errors:

    PruebaPunterosPorReferenciaC.c:6:29: error: expected ')'
    void pasoPorReferencia(int *&referencia){
                        ^
    PruebaPunterosPorReferenciaC.c:6:23: note: to match this '('
    void pasoPorReferencia(int *&referencia){
                  ^
    PruebaPunterosPorReferenciaC.c:6:29: error: parameter name omitted
    void pasoPorReferencia(int *&referencia){

Why does not that syntax compile in C? How to adapt it to C?

    
asked by Diego 16.09.2017 в 16:02
source

1 answer

6

First of all, standard C simply does not allow the step by reference, everything is passed by value . When a variable is passed as an argument to a function, what is passed is its value and that value is copied locally into the function. However, we can emulate the step by reference using pointers.

To achieve the same result you only need to pass a pointer pointing to the pointer you want to pass by reference. You still go through value at all times, what you do is pass the value of a pointer that points to the pointer dir_entero , that is, we actually pass to the function the memory address where% is located dir_entero . This variable is copied in pasoPorReferencia locally and used to modify the memory address pointed to by the pointer pasoPorReferencia using the dereference operator ( * ).

#include <stdio.h>

void pasoPorReferencia(int **referencia)
{
    int *dir_cambio, m = 2;
    dir_cambio = &m;
    *referencia = dir_cambio;
}

int main(int argc, char const *argv[])
{
    int *dir_entero, n = 13;

    dir_entero = &n;

    printf("Valor al que apunta dir_entero: %i\n", *dir_entero);
    pasoPorReferencia(&dir_entero);
    printf("Valor al que apunta dir_entero tras llamar a pasoPorreferencia: %i\n", *dir_entero);

    return 0;
}

See compiler online: link

Do not be confused with the use of a pointer to pointer, the mechanism is the same as when we intend to emulate the step by reference of a int or a double , for example.

#include <stdio.h>
#include <math.h>


void elevar_al_cubo(double *n)
{
    *n = pow(*n, 3);
}

int main(int argc, char const *argv[])
{
    double n = 5;
    printf("%0.f elevado a 3 es ", n);
    elevar_al_cubo(&n);
    printf("%0.f.\n", n);
    return 0;
}

It must be remembered that although C ++ inherits many C concepts and is largely compatible with C, they are two different languages. Among its many differences is that C ++ does allow the passage by real reference of a variable to a function or method.

    
answered by 16.09.2017 / 17:23
source