I am writting a program to shift the numbers entered in a circular manner which i know how to by using call by value , but i am unable to the logic in call by refrence way
This is the code:
#include<stdio.h>
int shift(int *a, int *b, int *c);
void main()
{
int x, y, z;
printf("Enter x: ");
scanf("%d", &x);
printf("Enter y: ");
scanf("%d", &y);
printf("Enter z: ");
scanf("%d", &z);
printf("Before Shift: x: %d\ty: %d\t z: %d",x,y,z);
//call the shift function
shift(&x, &y, &z);
printf("\n After Shift: x: %d\ty: %d\t z: %d",x,y,z);
}
int shift(int *a, int *b, int *c)
{
int temp;
temp = *c;
*c = *b;
*b = *a;
*a = temp;
}
here in the last part of the code,what are the exact meaning of the shift function and how is here the address is moving from one variable to another.
In C, variables are passed-by-value into functions. See What's the difference between passing by reference vs. passing by value? Specifically, let us re-write the programme for illustrative purposes.
The call to
failfrommaincopies the values(x, y, z)to(t, u, v).(t, u, v)are permuted, but then destroyed when the function goes out of scope andfailreturns tomain. The original values are unchanged.The programme then calls
shiftfrommainwith the addresses of(&x, &y, &z)passed to(a, b, c); dereferencing(*a, *b, *c)inshiftis(x, y, z)inmain. The variables are correctly permuted.