I have a question and want to receive your advices. Please help me. I am writing some codes with using pointer. My code bellow:
#include <stdio.h>
#include <stdlib.h>
void test_pointer (char *t);
int main (){
char *t = "abcdef";
printf("Before excuting func: %s\n", t);
test_pointer(t);
printf("After excuting func: %s\n", t);
return 0;
}
void test_pointer (char *t) {
printf("In function - before allocating: %s\n",t);
t =(char *) malloc(10);
t = "123456789";
printf("In function - after allocating: %s\n",t);
}
In this main(), I declared a char pointer and let its pointed to a string "abcdf" (length 7). But in the test_pointer() function, I used malloc() for allocating a new memory partition for that char pointer and then assigned a new value. That means char pointer had to be updated after using this function. But in real, it's not. Why?
when I executed this code, what I got is: Before excuting func: abcdef In function - before allocating: abcdef In function - after allocating: 123456789 After excuting func: abcdef
Please help me figure out what happened. Thank you very much.