How does a C program to swap elements using call by reference work?

Courtesy: Programiz.com 


Here, the three numbers entered by the user are stored in variables ab and c respectively. The addresses of these numbers are passed to the cyclicSwap() function.

cyclicSwap(&a, &b, &c);

In the function definition of cyclicSwap(), we have assigned these addresses to pointers.

cyclicSwap(int *n1, int *n2, int *n3) {
    ...
}

When n1n2 and n3 inside cyclicSwap() are changed, the values of ab and c inside main() are also changed.

Note: The cyclicSwap() function is not returning anything.

Comments

Popular posts from this blog

C program to read and display book information using structure

Count positive, negative and zero values in an array

Maximum value from array