I want to pass a char ** pointer by value. Given the following code:
char **ptr;
void init(){
int i;
ptr = (char**)malloc(2 * sizeof(char*));
for(i=0;i<2;i++){
ptr[i]=(char*)malloc(3 * sizeof(char));
ptr[i] = ..//assign different strings of length 2
}
}
void swap(char **x, char **y) {
char *temp;
temp = *x;
*x = *y;
*y = temp;
}
void f(char **ptr1){
//swap the first and second element
swap(&ptr1[0],&ptr1[1]);
}
int main{
init();
f(ptr);
}
I call the function f with ptr. I want to keep ptr's first values and want to use ptr1 as a copy of it. However, after the swap operation both ptr and ptr1 becomes same. ptr changes. How can I pass it by value?