H
hermit_crab67
Can someone explain to a C newbie why this doesn't work as I expect it
to work? (expectations clearly outlined in the printf statement in
main routine)
OS: Linux 2.4.26
GCC: 2.95.4
void modify_pointer(char *);
int main(int argc, char *argv[]) {
char *ptr_to_string;
modify_pointer(ptr_to_string);
printf("but why doesn't this say hello? %s\n", ptr_to_string);
}
void modify_pointer(char *the_ptr) {
the_ptr = (char *) malloc(strlen("hello"));
strcpy(the_ptr, "hello");
printf("this says hello: %s\n", the_ptr);
}
--
output:
this says hello: hello
but why doesn't this say hello? p�
---
to work? (expectations clearly outlined in the printf statement in
main routine)
OS: Linux 2.4.26
GCC: 2.95.4
void modify_pointer(char *);
int main(int argc, char *argv[]) {
char *ptr_to_string;
modify_pointer(ptr_to_string);
printf("but why doesn't this say hello? %s\n", ptr_to_string);
}
void modify_pointer(char *the_ptr) {
the_ptr = (char *) malloc(strlen("hello"));
strcpy(the_ptr, "hello");
printf("this says hello: %s\n", the_ptr);
}
--
output:
this says hello: hello
but why doesn't this say hello? p�
---