P
__PaTeR
I have a var in function main. I want to change its value... just
passing it in a new function. Following the faqs, i found out that c
only passes arguments by value, not by reference. I build up this code
to make things work.
void f(char **ipp) {
static char *b = "B";
printf("f %x %x %x\n", &ipp, ipp, *ipp);
printf("f %x %x %x\n", &b, b, *b);
*ipp = b;
printf("f %x %x %x\n", &ipp, ipp, *ipp);
}
int main() {
char *a = "A";
printf("%x %x %x\n", &a, a, *a);
f(&a);
printf("%x %x %x\n", &a, a, *a);
}
it works... but there are few things i still don't understand. this
function has as argument the address of the pointer to the stack
address of the string... in function "f" i just overwrite the value of
the a variable ( that is the pointer to the stack address ) with the
address of string stored where b points to.
No doubts in this... but i wrote another code that i don't know why it
doesn't work:
void g(char *ipp)
{
int *g = &ipp;
printf("g %x %x %x\n", &g, g, *g);
memset(*g, 'B', 1);
printf("g %x %x %x\n", &g, g, *g);
}
int main() {
char *a = "A";
printf("%x %x %x\n", &a, a, *a);
g(a);
printf("%x %x %x\n", &a, a, *a);
}
this time i pass the value. As far as i know, when you create a
string, for example
char *a = "HELLO";
you have this:
a = a pointer to the first char of the string in the stack
*a = the first char itself
&a = the address where the stack address for "H" is stored.
So, if i pass as argument simply "a", i should pass the address of the
string start in the stack. In effect, i pass that. So, continuing, i
assign the pointer to an int value... Now, "g" contains the pointer to
the start address.
In the function "f", i modified the pointer value... now i'm trying to
modify directly the data... why does it go in segmentation fault?
passing it in a new function. Following the faqs, i found out that c
only passes arguments by value, not by reference. I build up this code
to make things work.
void f(char **ipp) {
static char *b = "B";
printf("f %x %x %x\n", &ipp, ipp, *ipp);
printf("f %x %x %x\n", &b, b, *b);
*ipp = b;
printf("f %x %x %x\n", &ipp, ipp, *ipp);
}
int main() {
char *a = "A";
printf("%x %x %x\n", &a, a, *a);
f(&a);
printf("%x %x %x\n", &a, a, *a);
}
it works... but there are few things i still don't understand. this
function has as argument the address of the pointer to the stack
address of the string... in function "f" i just overwrite the value of
the a variable ( that is the pointer to the stack address ) with the
address of string stored where b points to.
No doubts in this... but i wrote another code that i don't know why it
doesn't work:
void g(char *ipp)
{
int *g = &ipp;
printf("g %x %x %x\n", &g, g, *g);
memset(*g, 'B', 1);
printf("g %x %x %x\n", &g, g, *g);
}
int main() {
char *a = "A";
printf("%x %x %x\n", &a, a, *a);
g(a);
printf("%x %x %x\n", &a, a, *a);
}
this time i pass the value. As far as i know, when you create a
string, for example
char *a = "HELLO";
you have this:
a = a pointer to the first char of the string in the stack
*a = the first char itself
&a = the address where the stack address for "H" is stored.
So, if i pass as argument simply "a", i should pass the address of the
string start in the stack. In effect, i pass that. So, continuing, i
assign the pointer to an int value... Now, "g" contains the pointer to
the start address.
In the function "f", i modified the pointer value... now i'm trying to
modify directly the data... why does it go in segmentation fault?