We all know that we can change the values of two integers x and y using the follwing code:
Code:
void interchange(int *u, int *v) {
int temp;
temp = *u;
*u = *v;
*v = temp;
}
int main() {
int x=5, y= 10;
interchange( &x, &y);
}
That is, you pass the address of x (&x) to a function that has a parameter *x.
What if x is itself a pointer?
Code:
void func ( char **array1, char *array ) {
array1 = array;
}
int main ( ) {
char *msg;
char *msg1;
msg = malloc(20);
sprintf(msg, "hello world");
func( &msg1, msg );
}
What I really want to achieve is msg1 = msg. But gcc isssues two warnings:
1. " assignment from incompatible pointer type" for 'array1 = array'
2. "passing arg 1 of `func' from incompatible pointer type" for 'func( &msg1, msg )'
And if I print msg1 at the end, I get a segmentation fault.
[Edit]Ooops. I got it. I should have said *array1 = array;
Now there is no seg fault. But warning #2 remains. Why?