I am programming in C. I send a structure as an argument to a function by "call by value method". Inside the function, I call another function which needs "call by reference" to the same structure. The call by value, should make a copy of structure and send a pointer of this copied version, to the inside function. My question is whether the original structure, outside both function will remain intact, no matter, how I play inside the functions.
Also, call by value, makes a copy of the structure. Is this very fast, compared to manually copying its member stuff, by using other algorithms like memcpy etc.
Example where function play changes the value
#include<stdio.h>
#include<stdlib.h>
typedef struct{
int dimension;
double *vector;
}myVector;
void changeVec(myVector *X)
{
X->vector[0]=100;
X->vector[1]=200;
}
void foo(myVector X)
{
changeVec(&X);
}
int main()
{
myVector X;
X.dimension=2;
X.vector=malloc(sizeof(double)*X.dimension);
X.vector[0]=1;
X.vector[1]=2;
printf("before function calling\n");
for(int i=0; i<X.dimension; i++)
{
printf("%f ",X.vector[i]);
}
printf("\n");
foo(X);
printf("after function calling\n");
for(int i=0; i<X.dimension; i++)
{
printf("%f ",X.vector[i]);
}
printf("\n");
return 0;
}